Get PID w/o JNI

R

Robert M. Gary

Does anyone know of a way to get the JVM's process ID (Windows and UNIX) w/o
using JNI? Does 1.5 have an interface for this?

-Robert
 
L

Lisa

Robert M. Gary said:
Does anyone know of a way to get the JVM's process ID (Windows and UNIX) w/o
using JNI? Does 1.5 have an interface for this?

-Robert

On windows XP the command is tasklist.
 
G

Gordon Beaton

Does anyone know of a way to get the JVM's process ID (Windows and
UNIX) w/o using JNI? Does 1.5 have an interface for this?

Here are three general solutions for Unix, one specific for Linux:

- run your program from a shell script like this, then get the pid
from the property:

#!/bin/sh
exec java -Dpid=$$ MyApp

- run a child process that can tell you the pid of its parent:

String[] cmd = { "/bin/bash", "-c", "echo $PPID" };
Process p = Runtime.getRuntime().exec(cmd);

then read the pid from p's InputStream.

- run ps from Runtime.exec() and parse the output.

- on Linux you can get your pid from the process file system. Use a
FileReader to read /proc/self/stat. Other unices have similar
factilities, however many use a binary format that isn't so easily
parsed from Java.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top