Java ClassLoader isVMAlive(String procId)

Here you can find the source of isVMAlive(String procId)

Description

is VM Alive

License

Open Source License

Declaration

public static boolean isVMAlive(String procId) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class Main {
    private static ClassLoader JVMToolClassloader = null;
    private static final Object lock = new Object();
    private static Class<?> virtualMachine;
    private static Class<?> virtualMachineDescriptor;
    private static Method method_VMList;
    private static Method method_AttachToVM;
    private static Method method_GetAgentProperties;
    private static Method method_VMId;
    private static Method method_GetSystemProperties;
    private static Method method_LoadAgent;
    private static Class<?> hostIdentifierClass;
    private static Class<?> monitoredVmClass;
    private static Class<?> vmIdentifierClass;

    public static boolean isVMAlive(String procId) {

        if (procId == null || "".equals(procId)) {
            return false;
        }//from w w w .ja v  a2  s .  c  om

        initJVMToolJarClassLoader();

        try {
            Object vm = method_AttachToVM.invoke(null, procId);
            if (vm != null) {
                return true;
            }
        } catch (Exception e) {
            // ignore
        }

        return false;
    }

    private static void initJVMToolJarClassLoader() {

        String javaHome = System.getProperty("java.home");

        String tools = javaHome + File.separator + ".." + File.separator + "lib" + File.separator + "tools.jar";

        if (JVMToolClassloader == null) {
            synchronized (lock) {
                if (JVMToolClassloader == null) {
                    try {
                        JVMToolClassloader = new URLClassLoader(new URL[] { new File(tools).toURI().toURL() });

                        // virtual machine
                        virtualMachine = JVMToolClassloader.loadClass("com.sun.tools.attach.VirtualMachine");
                        virtualMachineDescriptor = JVMToolClassloader
                                .loadClass("com.sun.tools.attach.VirtualMachineDescriptor");

                        method_VMList = virtualMachine.getMethod("list", (Class[]) null);
                        method_AttachToVM = virtualMachine.getMethod("attach", String.class);
                        method_GetAgentProperties = virtualMachine.getMethod("getAgentProperties", (Class[]) null);
                        method_VMId = virtualMachineDescriptor.getMethod("id", (Class[]) null);
                        method_GetSystemProperties = virtualMachine.getMethod("getSystemProperties",
                                (Class[]) null);
                        method_LoadAgent = virtualMachine.getMethod("loadAgent", new Class[] { String.class });

                        // java process
                        hostIdentifierClass = JVMToolClassloader.loadClass("sun.jvmstat.monitor.HostIdentifier");
                        monitoredVmClass = JVMToolClassloader.loadClass("sun.jvmstat.monitor.MonitoredVm");
                        vmIdentifierClass = JVMToolClassloader.loadClass("sun.jvmstat.monitor.VmIdentifier");

                    } catch (Exception e) {
                        // ignore
                    }
                }
            }
        }
    }
}

Related

  1. CreateLogbackXML(OutputStream out)
  2. findFile(String filename)
  3. hasImageFile(String name)
  4. hasLogDatePatternMatcher(final String line)
  5. isExist(String path)
  6. load(File propsFile)
  7. load(final String filename)
  8. load(String propertiesName)
  9. load(String propsName)