java 获取当前进程pid,判断是否是debug

package tool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Pattern;

public final class PID {
    private static volatile String pid;
    private static volatile String parentPid;
    private static volatile String parentProcessName;

    enum ProcessAttr {
        PROCESS_ID("ProcessId"),
        PARENT_PROCESS_ID("ParentProcessId"),
        NAME("Name"),
        OS_NAME("OSName"),
        STATUS("Status"),
        THREAD_COUNT("ThreadCount"),
        WINDOWS_VERSION("WindowsVersion"),
        SESSION_ID("SessionId");

        final String value;

        ProcessAttr(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return value;
        }
    }

    /**
     * Get current process ID
     *
     * @return
     */
    public static String getPID() {
        if (pid == null) {
            synchronized (PID.class) {
                if (pid == null) {
                    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
                    String processName = runtimeMXBean.getName();
                    if (processName.indexOf('@') != -1) {
                        pid = processName.substring(0, processName.indexOf('@'));
                    } else {
                        pid = null;
                    }
                }
            }
        }
        return pid;
    }

    /**
     * Get parent process id
     *
     * @param pid
     * @return
     */
    private static String getParentPID(String pid) {
        if (parentPid == null) {
            synchronized (PID.class) {
                if (parentPid == null) {
                    parentPid = getProcessAttr(pid, ProcessAttr.PARENT_PROCESS_ID);
                }
            }
        }
        return parentPid;
    }

    /**
     * Get process information
     *
     * @return
     */
    public static String getProcessAttr(String pid, ProcessAttr attrKey) {
        String result = null;
        if (pid == null || attrKey == null) {
            return null;
        }

        BufferedReader br = null;
        try {
            String cmd = "wmic process where ProcessId=" + pid + " get " + attrKey;
            br = new BufferedReader(
                    new InputStreamReader(
                            Runtime.getRuntime().exec(cmd).getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (line.length() > 0) {
                    result = line;
                }
            }
        } catch (IOException e) {
            Logger.getGlobal().severe(e.getMessage());
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    Logger.getGlobal().severe(e.getMessage());
                }
            }
        }
        return result;
    }


    /**
     * Determine if the application is opened with window explorer
     *
     * @return
     */
    public static boolean isStartWithWindowExplorer() {
        return isStartWithContain("explorer.exe");
    }

    /**
     * Determine if it is running in the specified process
     *
     * @param pidNameRegex
     * @return
     */
    public static boolean isStartWithContain(String... pidNameRegex) {
        String parentProcessName = getParentProcessName(getPID());
        if (parentProcessName == null || parentProcessName.isEmpty()) {
            return false;
        }
        for (String name : pidNameRegex) {
            Pattern pattern = Pattern.compile(name);
            if (pattern.matcher(parentProcessName).matches()) {
                return true;
            }
        }
        return false;
    }

    /**
     * Get parent process name
     *
     * @param pid
     * @return
     */
    private static String getParentProcessName(String pid) {
        if (parentProcessName == null) {
            synchronized (PID.class) {
                if (parentProcessName == null) {
                    parentProcessName = getProcessAttr(getParentPID(pid), ProcessAttr.NAME);
                }
            }
        }
        return parentProcessName;
    }

    /**
     * Determine whether it is currently in debug mode
     *
     * @return
     */
    public static boolean isDebug() {
        List args = ManagementFactory.getRuntimeMXBean().getInputArguments();
        for (String arg : args) {
            if (arg.startsWith("-Xrunjdwp") || arg.startsWith("-agentlib:jdwp")) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(getPID());
        System.out.println(isStartWithContain("idea\\d+.exe"));
        System.out.println(isDebug());
    }
}

 

你可能感兴趣的:(java)