JAVA代码片断:获取当前进程的PID

  private static String getPid() throws IOException {
        Process p = Runtime.getRuntime().exec("/home/kent/opt/jdk1.6.0_41/bin/jps");
        InputStream in = p.getInputStream();
        List<String> jpsLines = IOUtils.readLines(in);
        IOUtils.closeQuietly(in);
        for (String line : jpsLines) {
            if (line.contains(HelloPoolSize.class.getSimpleName())) {
                return line.split("\\s")[0];
            }
        }
        throw new IllegalStateException("拿不到pid");
    }

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.