Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give the java process a name in the operating system (other than java)

Tags:

java

I'm playing around with some microservices and running them on my laptop, simply assigning each micro-service a new port. The problem is that I'd like to restart one of them I have to close them all because in the operating system the processes are all called java. And although I sometime can guess that the last started have the highest pid etc is isn't exacly a safe bet...

So, is there a way to start a java-application and assign it a name in the operating system? Perhaps something like

java --Dos.name MyFirstService -jar MyJar.jar.

like image 270
Roland Avatar asked Oct 17 '15 12:10

Roland


People also ask

How can you identify the process in Java?

You can also find the PID of a java program with the task manager. You enable the PID and Command Line columns View -> Select Columns and are then able to find the right process.

How can I see all Java processes in Linux?

On Linux, you can view processes with the ps command. It is the simplest way to view the running processes on your system. You can use the ps command to view running Java processes on a system also by piping output to grep .

How do I list all Java processes in Windows?

To get a list of Java processes running on a machine, use either the ps command or, if the JVM processes are not running in a separate docker instance, the jps command.


2 Answers

Under Windows, you can't (unless installing some kind of posix subsystem).

Under Linux, you could use exec command with the -a "newName" option to alias the process you wish to spawn.

Like

exec -a "myJar" /path/to/java -jar /path/to/jar.jar
like image 78
Victor - Reinstate Monica Avatar answered Oct 10 '22 06:10

Victor - Reinstate Monica


If you need to be able to differentiate between different java programs you can use the jps command that gives you a list of all java processes and running your program with

java -Dname=myFirstService -cp  myFirstService.jar some.client.main.MyFirstService

then if you do a:

jps -v

You will see your process correctly.

If you need to change the process name at the OS level I recommend you use http://launch4j.sourceforge.net/

like image 37
Federico M. Rinaldi Avatar answered Oct 10 '22 08:10

Federico M. Rinaldi