PID of java process

30 views
Skip to first unread message

Rahul.V.

unread,
Jul 14, 2010, 7:07:56 AM7/14/10
to bojug
Hi,
Am working in the LInux(Fedora 12) environment.
Which is the easiest way to extract the PID of the current process in java?
I have found a couple of ways but they advocate using shell..
Is there any package for it??

--
Regards,
R.V.

Yaswanth Ravella

unread,
Jul 14, 2010, 9:31:37 AM7/14/10
to bo...@googlegroups.com
Hi, 

I guess, jps is not a solution for you. Can you mention what are those couple of ways that you have tried / found out ?

For retrieving PID, i think the only way is via JNI. That's how Sun does in their VMManagementImpl Class. 

So for retrieving the PID you may have to write your own native code. or simply use Sun API ( Restricted. Not accessible in a normal fashion ). You can still use the Sun API via Reflection. Not Advised. 

Below is the sample code.  Also attached. 


import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import sun.management.ManagementFactory;

/**
 * 
 * @author Yaswanth
 *
 */
public class ProcessID {

public static void main(String[] args) throws SecurityException, NoSuchFieldException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
System.out.println("PID :  "+getProcessId());
}
/**
* @return PID , -1 in case of error
*/
public static int  getProcessId(){
try {
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
Class runtimeImplClass = Class.forName("sun.management.RuntimeImpl");
Field jvmField = runtimeImplClass.getDeclaredField("jvm");
jvmField.setAccessible(true);
Class vMManagementImplClass = Class.forName("sun.management.VMManagementImpl"); 
Method getVmIdMethod = vMManagementImplClass.getDeclaredMethod("getProcessId");
getVmIdMethod.setAccessible(true);
Object vMManagementImplObject = jvmField.get(mxBean);
return (Integer) getVmIdMethod.invoke(vMManagementImplObject);
} catch (Exception e) {
System.err.println("ERROR");
}
return -1;
}

}



In case, If you are running under a security manager then make sure you have ReflectionPermission . 

Let me know if you have any improvements / comments. 

--
Yaswanth Ravella


--
You received this message because you are subscribed to the Google Groups "Bangalore Open Java Users Group- BOJUG" group.
To post to this group, send email to bo...@googlegroups.com.
To unsubscribe from this group, send email to bojug+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/bojug?hl=en.

ProcessID.java

Rahul.V.

unread,
Jul 14, 2010, 12:08:57 PM7/14/10
to bo...@googlegroups.com
Hi,
Thanks a lot for your help.
The easiest that I have found is using the system file /proc/self/stat.
The first entry in the file gives me the IP of the java process..
Am not sure however if this method is fullproof..i.e. if two or more java processes are running.
Another way which I have not explored completely is using a perl script in the java file.
Regards,
Rahul.V.
--
Regards,
R.V.

Viren

unread,
Jul 14, 2010, 11:54:19 AM7/14/10
to bo...@googlegroups.com
hi,

have a look at  this URL
http://blog.igorminar.com/2007/03/how-java-application-can-discover-its.html
thanks
viren

Nagabhirava Veerendra
+91 8028441355[HOME]
+91 9972788755 [CELL  ]
http://www.facebook.com/VirensJavaJump
http://www.twitter.com/VirensJavaJump


On Wed, Jul 14, 2010 at 7:01 PM, Yaswanth Ravella <ma...@yaswanth.org> wrote:
Reply all
Reply to author
Forward
0 new messages