Search in sources :

Example 1 with VMManagement

use of sun.management.VMManagement in project jdk8u_jdk by JetBrains.

the class ProcessTools method getProcessId.

/**
     * Get the process id of the current running Java process
     *
     * @return Process id
     */
public static int getProcessId() throws Exception {
    // Get the current process id using a reflection hack
    RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
    Field jvm = runtime.getClass().getDeclaredField("jvm");
    jvm.setAccessible(true);
    VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
    Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId");
    pid_method.setAccessible(true);
    int pid = (Integer) pid_method.invoke(mgmt);
    return pid;
}
Also used : Field(java.lang.reflect.Field) VMManagement(sun.management.VMManagement) RuntimeMXBean(java.lang.management.RuntimeMXBean) Method(java.lang.reflect.Method)

Example 2 with VMManagement

use of sun.management.VMManagement in project quasar by puniverse.

the class ProcessUtil method getCurrentPid.

public static int getCurrentPid() {
    try {
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        Field jvmField = runtimeMXBean.getClass().getDeclaredField("jvm");
        jvmField.setAccessible(true);
        VMManagement vmManagement = (VMManagement) jvmField.get(runtimeMXBean);
        Method getProcessIdMethod = vmManagement.getClass().getDeclaredMethod("getProcessId");
        getProcessIdMethod.setAccessible(true);
        return (Integer) getProcessIdMethod.invoke(vmManagement);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Field(java.lang.reflect.Field) VMManagement(sun.management.VMManagement) RuntimeMXBean(java.lang.management.RuntimeMXBean) Method(java.lang.reflect.Method)

Example 3 with VMManagement

use of sun.management.VMManagement in project jdk8u_jdk by JetBrains.

the class JdpController method getProcessId.

// Get the process id of the current running Java process
private static Integer getProcessId() {
    try {
        // Get the current process id using a reflection hack
        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
        Field jvm = runtime.getClass().getDeclaredField("jvm");
        jvm.setAccessible(true);
        VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
        Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId");
        pid_method.setAccessible(true);
        Integer pid = (Integer) pid_method.invoke(mgmt);
        return pid;
    } catch (Exception ex) {
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) VMManagement(sun.management.VMManagement) RuntimeMXBean(java.lang.management.RuntimeMXBean) Method(java.lang.reflect.Method) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Aggregations

RuntimeMXBean (java.lang.management.RuntimeMXBean)3 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 VMManagement (sun.management.VMManagement)3 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1