63

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Mnementh
  • 50,487
  • 48
  • 148
  • 202

8 Answers8

52

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.

slm
  • 15,396
  • 12
  • 109
  • 124
omerkudat
  • 9,371
  • 4
  • 33
  • 42
  • Not exactly the answer for that I asked, but exactly the right answer for my problem. Thanks. :-) – Mnementh Jun 29 '09 at 10:20
  • For lack of setproctitle this is a clever solution. Kudos. – synthesizerpatel Jan 26 '12 at 05:27
  • 1
    I am also looking for similar options. But this answer is not clear to me. Can somebody explain me how to set the process name. I saw there is an option called '-D=value'. Do we need to use this option? Thanks. – Naresh Jul 17 '12 at 08:10
  • 3
    +1 for the -D idea, doesn't get much quicker or simpler than that – jsh Dec 04 '12 at 17:33
  • Can't seem to get this working in Windows. What is the correct command? – Yster Oct 09 '16 at 16:19
25

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.

d.raev
  • 9,216
  • 8
  • 58
  • 79
15

You can do this with an LD_PRELOAD shim: https://github.com/airlift/procname

The shim simply calls the Linux-specific prctl() when the process starts:

static void __attribute__ ((constructor)) procname_init()
{
   prctl(PR_SET_NAME, "myname");
}

The call has to happen on the main thread, so it isn't possible to do this from Java or even with a JVMTI agent, since those happen on a different thread.

David Phillips
  • 10,723
  • 6
  • 41
  • 54
  • 1
    This works really well and is a simpler approach than most here – D2TheC Feb 19 '16 at 06:33
  • I would consider using this code if the code were licensed under MIT, Apache, or some other such license agreement. – Travis Spencer Feb 20 '16 at 09:11
  • 7
    This code is so trivial (it only calls one function) that it doesn't even seem copyrightable. Please consider it public domain. If you want, I can add a LICENSE file. – David Phillips Feb 21 '16 at 03:07
5

When I first read this, the idea of changing the process name struck me as impossible. However, according to this ancient thread on the sun forum you can use C++ wrappers around the JVM executable to achieve this.

Though frankly, I wonder what your real problem is, as I'd guess there is a more standard solution then attempting to change the process name.

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
  • 4
    If you had to kill your application and had several Java applications running it would be a little hit and miss. – Lloyd Jun 29 '09 at 09:02
  • 2
    You're right, the jps-command mentioned by omerkudat solve the problem without renaming the processes. – Mnementh Jun 29 '09 at 11:30
  • 3
    creating a little native JNI wrapper to launch an app is also very easy. For those of us that do Windows deployment, it allows for auto-configuration of the JVM, custom icons, etc... It's really not hard to do. – Kevin Day Jun 30 '09 at 03:52
  • As usual, ancien link got removed. – pdem Sep 12 '18 at 09:54
  • 2
    @pdem see this question: https://stackoverflow.com/questions/33186661/how-to-give-the-java-process-a-name-in-the-operating-system-other-than-java/33186764 The gist is use launch4j. My apologies for making an answer that is just a link. I was young and did not know any better. – Tim Bender Sep 20 '18 at 23:36
5

Your best option is something like launch4j http://launch4j.sourceforge.net/

There is a bug logged in the sun bugtracker for this, but it's not high priority http://bugs.sun.com/view_bug.do?bug_id=6299778

Noel Grandin
  • 3,143
  • 25
  • 17
3

There are mainly 2 approaches: one is as already described: using tools like Launch4j, WinRun4J to create native Windows launchers.

Another approach that seems better is to use Apache Procrun to wrap the java application as a Windows service. During the install service process, we can give the process an meaningful name such as OurApp.exe.

All we need do is rename prunsrv.exe to OurApp.exe and replace every occurrence of prunsrv.exe in our install|start|stop|uninstall service scripts to MyApp.exe.

See more from Using Apache Procrun to Rename Process Name of a Java Program in Windows

jeffery.yuan
  • 1,177
  • 1
  • 17
  • 27
  • Launch4J will not solve the problem. They disabled the ability to rename processes. Additionally it doesn't account for multi process applications, like when running javaw and browsercore. – gunslingor May 29 '19 at 13:15
2

If you want to use a different process name you'll have to create your own binary to launch your Java application using something like JSmooth.

Look at this question for a discussion of creating such binaries.

Community
  • 1
  • 1
David Webb
  • 190,537
  • 57
  • 313
  • 299
1

That's because Java applications aren't actually executable they're ran by the Java virtual machine which is why java appears in the process monitor, it's the host of your application.

Things like LimeWire however do but I think that's more down to GCJ - http://gcc.gnu.org/java/

Lloyd
  • 29,197
  • 4
  • 84
  • 98