问题
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?
回答1:
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.
回答2:
Check out launch4j, it is an executable wrapper that allows you to assign executable names.
回答3:
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.
- Launch4j, looks to be the most recent and up to date
- JSmooth
- install4J, commercial, more than you need
回答4:
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.
回答5:
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.
回答6:
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.
回答7:
Not all flavors of exec support the -a flag. If yours doesn't, the argv0 program does something similar.
回答8:
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.
来源:https://stackoverflow.com/questions/882826/how-to-change-the-name-of-a-java-application-process