aspose file tools*
The moose likes Java in General and the fly likes How to get processId of calling process in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get processId of calling process in Java" Watch "How to get processId of calling process in Java" New topic
Author

How to get processId of calling process in Java

rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
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??


Sage of The Monstrous Toad of Mount Myoboku
Ed Ward
Ranch Hand

Joined: Jan 30, 2006
Posts: 147
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

Joined: Oct 13, 2009
Posts: 156
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ed Ward
Ranch Hand

Joined: Jan 30, 2006
Posts: 147
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

Joined: Oct 13, 2005
Posts: 39409
    
  28
Too difficult for a "beginning" topic. Moving thread.
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Hey anyone please tell me is there any API for getting PID of calling process in java........???
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Not in Java.

In Windows, the native API call is GetCurrentProcessId() . In Linux, it's getpid().


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
So do i required to import windows/linux library and then get processID??
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get processId of calling process in Java