Java - Process

1 - About

Os - Process in Java

3 - Management

3.1 - Get Pid

import sun.management.VMManagement;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
 
public static int getCurrentPID() {
    try{
        java.lang.management.RuntimeMXBean runtime = 
            java.lang.management.ManagementFactory.getRuntimeMXBean();
        java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm");
        jvm.setAccessible(true);
        sun.management.VMManagement mgmt = 
           (sun.management.VMManagement) jvm.get(runtime);
        java.lang.reflect.Method pid_method = 
            mgmt.getClass().getDeclaredMethod("getProcessId");
        pid_method.setAccessible(true);
        return (Integer) pid_method.invoke(mgmt);
    }
    catch(Exception e){
        e.printStackTrace();
        System.out.println("Failed at getting the process ID");
        System.exit(0);
    }
}

https://stackoverflow.com/questions/11685408/java-get-pid-of-external-process-by-command-line-in-windows-7

3.2 - List

jps -lv | findstr junit
24060 com.intellij.rt.execution.junit.JUnitStarter -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:C:\Idea2018\lib\idea_rt.jar=12480:C:\Idea2018\bin -Dfile.encoding=UTF-8
lang/java/process.txt ยท Last modified: 2018/09/15 14:54 by gerardnico