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!");
}
}
}
}