Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the process name for a Java-program? [duplicate]

Tags:

java

process

If a Java program is started, it get's in the system process-monitor the name java. Many Java-programs are that way hard to distinguish. So it would be nice, if a way exists, to set the name, that will be shown in the process-monitor. I'm aware that this may work different on different Operating Systems.

A simple way would be, if the java-interpreter would support a switch to set the name, like this:

java -processname MyProgram -jar MyProgram 

But I couldn't find such a switch, so it is probably non-existant. An API in Java to set the process-name would be also fine.

So, so you have any suggestions?

like image 716
Mnementh Avatar asked Jun 29 '09 08:06

Mnementh


People also ask

How can a Java program gets its own process ID?

getRuntimeMXBean(). getName() looks like the best (closest) solution, and typically includes the PID. It's short, and probably works in every implementation in wide use. On linux+windows it returns a value like "12345@hostname" ( 12345 being the process id).

How do you create a new process in Java?

There is only one way to create processes in Java, Runtime. exec() - basically it allows you to start a new JVM just as you would via the command line interface.


2 Answers

as @omerkudat said:

jps -v 

prints out all java processes {processID, params list} If the params list is not enough to recognize the applications you need, try adding some dummy params when running them:

java -Dname=myApp -cp  myApp.jar some.client.main.MainFrame 

This will print like:

7780 MainFrame -Dname=myApp 

and you can use the process ID to kill / monitor it.

like image 36
d.raev Avatar answered Oct 02 '22 00:10

d.raev


I don't know if this is possible, but you could use a command line tool that comes with the JDK called 'jps'. It's like *nix ps, but just Java programs instead. jps -v shows all the arguments you have passed to java.

Also, I have seen people attach a "process name" to their java processes by adding an unused -Dmyprocessname to the args.

like image 186
omerkudat Avatar answered Oct 01 '22 22:10

omerkudat