I need to write a program that create some process by select number from menu. And then, if I select 4, ALL process created by this program will be terminate.

So, I'm using processBuilder to create process.
The problem is how can I destroy the process?

Thank you.

Here's is my code,

import java.io.IOException;
import java.util.Scanner;

public class process1{
    public static void main(String[] args)
    {
		final int MAX_PROCESS = 100;
		Scanner scan = new Scanner(System.in);
		int choice;
		boolean cont = true;
		ProcessBuilder proc[] = new ProcessBuilder[MAX_PROCESS];
		int numOfProcess = 0;

		while(cont)
		{
			System.out.println("---1. Calculator---");
			System.out.println("---2. Notepad   ---");
			System.out.println("---3. Paint     ---");
			System.out.println("---4. Terminate ---");

			choice = scan.nextInt();

			switch(choice)
			{
				case 1:
					try
					{
						proc[numOfProcess] = new ProcessBuilder("calc.exe");
						proc[numOfProcess].start();
					}
					catch (IOException e)
					{
						e.printStackTrace();
					}
					break;
				case 2:
					try
					{

						proc[numOfProcess] = new ProcessBuilder("notepad.exe");
						proc[numOfProcess].start();
					}
					catch (IOException e)
					{
						e.printStackTrace();
					}
					break;
				case 3:
					try
					{

						proc[numOfProcess] = new ProcessBuilder("paint.exe");
						proc[numOfProcess].start();

					}
					catch (IOException e)
					{
						e.printStackTrace();
					}
					break;
				case 4:
					//for(int i=0; i<numOfProcess; i++)
					//	proc[i].destroy;
					cont=false;
					break;
				default:
					System.out.println("Error: Please input correct selection!");
			}

		}
  }
}

Recommended Answers

All 7 Replies

You need to retain the Process returned by the start() method. The Process objects has a destroy() method that you can use to kill it.

You should change your "proc" array to be a Process array instead of ProcessBuilder. You don't need to keep the ProcessBuilder around for anything after you have created the process.

You need to retain the Process returned by the start() method. The Process objects has a destroy() method that you can use to kill it.

You should change your "proc" array to be a Process array instead of ProcessBuilder. You don't need to keep the ProcessBuilder around for anything after you have created the process.

Process can't declare be array:-/,
because process is belong to java.lang.object.
That's why I'm using ProcessBuilder to create process...

Let's view result

ProcessBuilder pb = new ProcessBuilder("calc.exe");
                        System.out.println(pb.getClass());
                        //even if you write
                        Object p = pb.start();// Object or not object?
                        System.out.println(p.getClass());
                        proc[choice] = (Process)p;
                        System.out.println(proc[choice].getClass());

where proc is declared as Process array (Ezzaral point of view).

I think process cant assign to an object.

I'm not sure what part you are confused about, but "process cant assign to an object" doesn't have anything to do with what I said.

This tiny example shows what I was referring to.

try {
    Process[] proc = new Process[2];
    proc[0] = new ProcessBuilder("calc.exe").start();
    proc[1] = new ProcessBuilder("notepad.exe").start();
    
    try {
        Thread.sleep(3000);
    } catch (InterruptedException ex) {
    }
    
    proc[0].destroy();
    proc[1].destroy();
    
} catch (IOException ioe) {
    ioe.printStackTrace();
}

It's work!!
Thanks for helping:)

Some explanation to a side issue - with some of the statements I can not agree

Process can't declare be array,
because process is belong to java.lang.object.
That's why I'm using ProcessBuilder to create process...

I think process cant assign to an object.

I recomended to read java tutorial: Inheritance, Casting Objects
http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html
They are more informative than me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.