简体   繁体   中英

java how get all processes launched by jar

In java I need to get processes launched by another jar.

A.jar -> B.jar ->some app(get Ids)

I have A.jar which launching B.jar, then B.jar launching some app (for example browser) I need get process id (PID) of this browser in A.jar and destroy this PID.

A.jar

public class A {
    public static void main(String[] args) {
        ProcessBuilder builder;
        Process process = null;
        try{
            List<String> command = new ArrayList<String>();
            
                        command.add("java");
                        command.add("-jar");
                        command.add("B.jar");

                        builder = new ProcessBuilder(command);          
                        process = builder.start();

                        ProcessHandle processHandle = process.toHandle();
                        destroyProcess(processHandle);
                        //System.exit(0);
                    }catch(Exception e){
            e.printStackTrace();            
         }
..
..
private static void destroyProcess(ProcessHandle processHandle){
// destroy app of B.jar
}

B.jar

public class B{
public static void main(String[] args){
  // launch browser
  ChromeDriver driver = new ChromeDriver(options);
}
}

thank you Slaw found solution

processHandle.children().forEach(proc -> proc.children().forEach(child -> System.out.println("PID: [ " + child.pid() + " ], Cmd: [ " + child.info().command() + " ]")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM