• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to get processId of calling process in Java

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe check the following thread in which I asked about identifying unique JVM instances.

https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...........??
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey anyone please tell me is there any API for getting PID of calling process in java........???
 
Marshal
Posts: 22450
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So do i required to import windows/linux library and then get processID??
 
Rob Spoor
Marshal
Posts: 22450
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic