• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get processId of calling process in Java

 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
I wanted to know How to get processId of calling process in Java??
I know processId is platform specific but i wonder if JAVA supports a library or function for getting processId independent of platform??
 
Ed Ward
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe check the following thread in which I asked about identifying unique JVM instances.

http://www.coderanch.com/t/508512/java/java/Unique-JVM-identifier

Seems to boil down to using the JNI API. Google does know about a few examples.
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ed, but your post says something about JVM instance..... I just wanted to know is there any API/function call to get calling process PID in java...........??
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The last time I checked, I couldn't find an API that returned the PID, but of course, they may have added one since then.

Back then, you had to use "ps" for unix, and "tasklist" for windows, and parse it to find your pid.

A better option, if you are using a batch file or shell script to start your JVM, is to have the script save the PID for you somewhere (like a file).

Henry
 
Ed Ward
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohan yadav wrote:Thanks Ed, but your post says something about JVM instance..... I just wanted to know is there any API/function call to get calling process PID in java...........??

Yes, I was asking about the JVM instance in that post, but the answer involved getting the PID.
You asked about PID, so I thought it might be relevant to your issue.
My mistake.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50281
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult for a "beginning" topic. Moving thread.
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey anyone please tell me is there any API for getting PID of calling process in java........???
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not in Java.

In Windows, the native API call is GetCurrentProcessId() . In Linux, it's getpid().
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So do i required to import windows/linux library and then get processID??
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use JNI. In short:
- write a native method
- use javah to create the header (.h) file
- write a matching .c source file with the implementation
- compile it (search a bit on how to do this for different operating systems)
- add a System.loadLibrary call to the class to load the library
- make sure the .dll (for Windows) or .so (for Linux) is found as part of the "java.library.path" system property
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic