How a Java Application Can Discover its Process ID (PID)
2014-06-06 13:00
435 查看
转自:http://www.blogjava.net/temper/archive/2010/07/01/324992.html
Occasionally it is important for an application to know its PID,specially if this application cooperates with other non-javaapplications. Currently there is no direct
support for retrieving anapplication's process id by using standard Java api (this might changein the future if RFEs like 4250622, 4244896 or 4890847 are
resolved).
I found five ways how to get the PID from my Java code:
Using the java management and monitoring API (java.lang.management):
returns something like:
where 28906 is the PID of JVM's process, which is in fact the PID of my app.
This hack is JVM dependent and I tested it only with Sun's JVM.
From Javadocs for getName() method of RuntimeMXBean:
Returns the name representing the running Java virtualmachine. The returned name string can be any arbitrary string and aJava virtual machine implementation can choose to embedplatform-specific useful information in the returned name string. Eachrunning
virtual machine could have a different name.
So even though this approach is the most comfortable one, your app can break if the implementation of this method changes.
Using shell script in addition to Java propertiesStart your app with a shellscript like this:
then in java code call:
Using shell script's $! facility as described on this blog - this approach is fine if all you want is to create
a pid file.
Using Java Native Interface (JNI) - a very cumbersome and platform dependent solution.
Using
post
It must be said that none of these approaches is perfect and each of them has some drawbacks.
I know that one of Sun's main priorities for Java is cross-platformcompatibility, but I have to agree with the comments on the RFEs abovewhich support the addition of a getPid()
method to JDK. I especiallylike this one:
Come on Sun, this started 4.5 years ago. Give us the
PID. We need it. We want it. We demand it.
And another thing ... you will save a lot of developers a
lot of time currently spent searching through the
documentation trying to find a getPID method that isn't
there!
没有一个平台独立的方法能够在所有的JVM上实现。一个最简单、最接近取得PID的办法是使用:
ManagementFactory.getRuntimeMXBean().getName() 。
取得到的字符窜的格式为[PROCESS_ID]@[MACHINE_NAME],通过解析这个字符串就可以得到java进程的PID。
在以下平台上测试通过:
1、Windows、Linux上的Sun JDK1.5、JDK6
2、HP-UX上的JDK1.5、JDK6
3、Linux上的JRockit R27.6
Occasionally it is important for an application to know its PID,specially if this application cooperates with other non-javaapplications. Currently there is no direct
support for retrieving anapplication's process id by using standard Java api (this might changein the future if RFEs like 4250622, 4244896 or 4890847 are
resolved).
I found five ways how to get the PID from my Java code:
Using the java management and monitoring API (java.lang.management):
ManagementFactory.getRuntimeMXBean().getName();
returns something like:
28906@localhost
where 28906 is the PID of JVM's process, which is in fact the PID of my app.
This hack is JVM dependent and I tested it only with Sun's JVM.
From Javadocs for getName() method of RuntimeMXBean:
Returns the name representing the running Java virtualmachine. The returned name string can be any arbitrary string and aJava virtual machine implementation can choose to embedplatform-specific useful information in the returned name string. Eachrunning
virtual machine could have a different name.
So even though this approach is the most comfortable one, your app can break if the implementation of this method changes.
Using shell script in addition to Java propertiesStart your app with a shellscript like this:
exec java -Dpid=$$ -jar /Applications/bsh-2.0b4.jar
then in java code call:
System.getProperty("pid");
Using shell script's $! facility as described on this blog - this approach is fine if all you want is to create
a pid file.
Using Java Native Interface (JNI) - a very cumbersome and platform dependent solution.
Using
$PPIDand
Runtime.exec(String[])method - described in detail in this
post
import java.io.IOException; public class Pid { public static void main(String[] args) throws IOException { byte[] bo = new byte[100]; String[] cmd = {"bash", "-c", "echo $PPID"}; Process p = Runtime.getRuntime().exec(cmd); p.getInputStream().read(bo); System.out.println(new String(bo)); } }
It must be said that none of these approaches is perfect and each of them has some drawbacks.
I know that one of Sun's main priorities for Java is cross-platformcompatibility, but I have to agree with the comments on the RFEs abovewhich support the addition of a getPid()
method to JDK. I especiallylike this one:
Come on Sun, this started 4.5 years ago. Give us the
PID. We need it. We want it. We demand it.
And another thing ... you will save a lot of developers a
lot of time currently spent searching through the
documentation trying to find a getPID method that isn't
there!
没有一个平台独立的方法能够在所有的JVM上实现。一个最简单、最接近取得PID的办法是使用:
ManagementFactory.getRuntimeMXBean().getName() 。
取得到的字符窜的格式为[PROCESS_ID]@[MACHINE_NAME],通过解析这个字符串就可以得到java进程的PID。
在以下平台上测试通过:
1、Windows、Linux上的Sun JDK1.5、JDK6
2、HP-UX上的JDK1.5、JDK6
3、Linux上的JRockit R27.6
private int getPid() { RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean(); String name = runtime.getName(); // format: "pid@hostname" try { return Integer.parseInt(name.substring(0, name.indexOf('@'))); } catch (Exception e) { return -1; } }
相关文章推荐
- How can I send an email by Java application?
- Bash: how to check if a process id (PID) exists
- Java How to get the PID from a process?
- How to discover memory usage of my application in Android
- 启动MyEclipse 出现java.lang.RuntimeException: No application id has been found 解决办法
- How To Configure Server Side Transparent Application Failover [ID 460982.1]
- How To Configure Server Side Transparent Application Failover [ID 460982.1]
- Java.lang.RuntimeException: No application id has been found.
- [Android代码] 读取Activity和Process的PID (Process ID)
- How To Configure Server Side Transparent Application Failover [ID 460982.1]
- 启动MyEclipse 出现java.lang.RuntimeException: No application id has been found 解决办法
- How Can I Get the Unique ID for the Last Inserted Row
- How can I handle hardware keys in my application?
- How to: How to disable Java Security Warning "The application requires an earlier version of Java."
- How to: How to disable Java Security Warning "The application requires an earlier version of Java."
- How to discover memory usage of my application in Android
- how can you see if an application program offers a function module exit
- How can I run an EXE from within my application?
- How To Configure Server Side Transparent Application Failover [ID 460982.1]
- [导入]How can I run another application or batch file from my Visual C# .NET code?