44

When executing a Java application the process name given to it is usually java.exe or javaw.exe. But how can I make it be called by the name of my application?

Tiago Sippert
  • 1,324
  • 7
  • 24
  • 33
  • related: [How to change argv0 in bash so command shows up with different name in ps?](http://stackoverflow.com/questions/3251550/how-to-change-argv0-in-bash-so-command-shows-up-with-different-name-in-ps) – jfs Nov 25 '16 at 23:34

10 Answers10

43

These methods are suited for servers with a lot of java processes running, and where you need a quick way of finding the correct jvm (not using jps.) For applications, I suppose launch4j or another wrapper is the way to go.

On unix, If you are launching from a shell sript (at least for bash and possibly for other decent shells) you can use:

exec -a goodname java ...

to launch java and pass "goodname" as the 0th argument, which will be shown as the process name in ps etc.

A perhaps better alternative (that seems to work also for top) is to create a symlink: ln -s /usr/bin/java /usr/local/bin/kallekula.

Shortcuts in windows won't do the trick, but windows vista/7 supports symlinks using mklink. That may work, but I haven't tested. I am not sure if exec -a also works with cygwin bash on Windows.

Quinn Taylor
  • 44,553
  • 16
  • 113
  • 131
KarlP
  • 5,149
  • 2
  • 28
  • 41
  • This is good for launching servers etc, as it is a simple edit in the start shell-script. – KarlP May 19 '09 at 15:04
  • No, I was totally in unix think when I read the question. I've edited the answer. – KarlP Sep 29 '11 at 17:05
  • 1
    I thought it did not work even on Ubuntu 11.04. As it turns out, it sets some name but not the "root" name. `ps x` shows the specified name as does `htop` of told to "Show Custom Thread Names`, but both show `java` by default. – Raphael Sep 29 '11 at 21:51
  • 1
    Oh. 'top' seems to show the executables name (the name of the file on the disk). 'ps' shows arg[0] of the command line. I think that top used to show that too, but maybe it's been changed for increased security. I suppose it's impossible to change the link to the exe. However, it's always possible to do a softlink with another line. I've updated the answer again. – KarlP Sep 30 '11 at 23:31
  • @KarlP how to change it in netbeans javafx project while building standalone exe file? – TejpalBh Mar 04 '19 at 06:05
18

Check out launch4j, it is an executable wrapper that allows you to assign executable names.

z -
  • 7,130
  • 3
  • 40
  • 68
  • 5
    The feature has been removed in 2013: Changes in version 3.1.0-beta2 (20-08-2013) Removed the custom process name feature which was not compatible with newer Windows versions. – Olivier Faucheux Nov 13 '14 at 12:51
  • 2
    It is true that launch4j no longer allows you to change the name of the javaw.exe process. However, if you create the launch4j exe using the option stayalive=true, then the process name for your custom exe file will remain in the list of processes in addition to the javaw.exe process. So, this gives users the option to kill the correct java process by killing the process with your custom name. – Enwired Sep 24 '15 at 21:34
  • Yes, tested in Windows 10. When it is true, the app name and its logo will be in the task manager. But the problem is if I kill the application by its custom name, it does not kill the application. – insyncim64 Nov 06 '15 at 15:14
  • 1
    Oh, sorry, I need to correct it again. You can kill the process by killing the whole process tree. It works. – insyncim64 Nov 06 '15 at 16:00
  • [WinRun4J](https://stackoverflow.com/a/60339473/3479450) supports a custom application process name. – user3479450 Feb 21 '20 at 13:55
11

This is specific to Windows.
I was facing the same issue where I have to kill the specific java program using taskkill. When I run the java program, tasklist was showing the same program with Image name set as java.exe. But killing it using taskkill /F java.exe will stop all other java applications other than intended one which is not required.

So I run the same java program using:

start "MyProgramName" java java-program..

Here start command will open a new window and run the java program with window's title set to MyProgramName.

Now to kil this java-program use the following taskkill command:

taskkill /fi "MyProgramName"

Your Java program will be killed only. Rest will be unaffected.

elite21
  • 757
  • 8
  • 9
  • 2
    Thank you that is exactly what I searched for. Just one small remark: `taskkill /fi "MyProgramName"` did not work for me. I had to add the filter criteria: `taskkill /fi "WINDOWTITLE eq MyProgramName"` – Florian Oct 30 '20 at 09:34
11

Unless you launch Java via JNI in your own custom built executable, the process name will always be java.exe.

There are several java launchers/wrappers that can generate this executable for you.

basszero
  • 29,624
  • 9
  • 57
  • 79
5

If you're using the Sun JDK, you can also use the "jps" command line tool to get a detailed list of Java processes running on the box.

Rob H
  • 14,502
  • 8
  • 42
  • 45
4

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
3

Assuming that what you are really after is a way to terminate the correct the correct process later on, then an alternative solution is this:

Run ps -ef | grep java and you should get a listing that looks something like this:

 mruser    7518  7505  4 11:37 pts/3    00:00:00 /usr/bin/java -classpath MRD3030_Linked.jar peralex.MyApp

Then pkill -f peralex.MyApp will kill the correct process.

Quinn Taylor
  • 44,553
  • 16
  • 113
  • 131
Noel Grandin
  • 3,143
  • 25
  • 17
  • 1
    Note that that only works if the /remembered/ command line isn't truncated by the system. Linux and Solaris both have limits on the length of the command line that is remembered (independent from the maximum that is accepted), and you easily exceed that with a long classpath. – ankon Apr 06 '11 at 15:13
2

Not all flavors of exec support the -a flag. If yours doesn't, the argv0 program does something similar.

mpm
  • 1,066
  • 9
  • 23
  • `argv0` does come installed by default, `argv0` simply calls `execve(2)` with prepared argument strings and its manual contains an example C code. – mariusm Mar 18 '20 at 12:01
1

I needed a workaround for this in windows to enable me to stop particular java processes. I've settled on using a .bat file to run the different .jar files, with the first line of the .bat file being TITLE "Name to give the cmd window", then when I look through the different cmd windows I can see which on is the one I want to stop and Ctrl-C via that cmd window.

0

WinRun4J

In many Java exe wrappers the custom exe is only a launcher and the process still runs as the usual java(w).exe. In WinRun4J on the other hand the custom exe is the process that runs your application, so for example in Task Manager your custom exe is what appears. It supports 32 bit and 64 bit, console and no console (along with numerous configuration options).

user3479450
  • 131
  • 2
  • 3