如何更改Java应用程序进程的名称?

时间:2021-12-18 16:25:38

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?

执行Java应用程序时,为其指定的进程名称通常是java.exe或javaw.exe。但是如何通过我的应用程序名称来调用它呢?

8 个解决方案

#1


38  

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.

这些方法适用于运行大量java进程的服务器,并且需要快速查找正确的jvm(不使用jps)。对于应用程序,我认为launch4j或其他包装器是可行的方法。

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

在unix上,如果你从shell sript(至少对于bash以及可能对于其他体面的shell)启动,你可以使用:

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.

启动java并将“goodname”作为第0个参数传递,它将在ps等中显示为进程名称。

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.

一个或许更好的替代方案(似乎也适用于top)是创建一个符号链接: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.

Windows中的快捷方式不起作用,但是windows vista / 7支持使用mklink的符号链接。这可能有用,但我还没有测试过。我不确定exec -a是否也适用于Windows上的cygwin bash。

#2


18  

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

查看launch4j,它是一个可执行的包装器,允许您分配可执行文件名。

#3


11  

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

除非您在自定义构建的可执行文件中通过JNI启动Java,否则进程名称将始终为java.exe。

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

有几个java启动器/包装器可以为您生成此可执行文件。

  • Launch4j, looks to be the most recent and up to date
  • Launch4j,看起来是最新的和最新的
  • JSmooth
  • JSmooth
  • install4J, commercial, more than you need
  • install4J,商业,超出您的需求

#4


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.

如果您正在使用Sun JDK,还可以使用“jps”命令行工具获取在该框上运行的Java进程的详细列表。

#5


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:

运行ps -ef | grep java,你应该得到一个看起来像这样的列表:

 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.

然后pkill -f peralex.MyApp会杀死正确的进程。

#6


3  

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.

这是Windows特有的。我遇到了同样的问题,我必须使用taskkill杀死特定的java程序。当我运行java程序时,tasklist显示了相同的程序,其Image名称设置为java.exe。但是使用taskkill / F java.exe杀死它将停止所有其他不需要的Java应用程序。

So I run the same java program using:

所以我运行相同的java程序使用:

start "MyProgramName" java java-program..

启动“MyProgramName”java java程序..

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:

这里start命令将打开一个新窗口并运行java程序,窗口标题设置为MyProgramName。现在来这个java程序使用以下taskkill命令:

taskkill /fi "MyProgramName"

taskkill / fi“MyProgramName”

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

您的Java程序将只被杀死。休息不受影响。

#7


2  

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

并非所有类型的exec都支持-a标志。如果你没有,argv0程序会做类似的事情。

#8


2  

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

您可以使用LD_PRELOAD垫片执行此操作:https://github.com/airlift/procname

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

当进程启动时,垫片只调用特定于Linux的prctl():

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.

调用必须在主线程上进行,因此无法从Java或甚至使用JVMTI代理执行此操作,因为这些操作发生在不同的线程上。

#1


38  

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.

这些方法适用于运行大量java进程的服务器,并且需要快速查找正确的jvm(不使用jps)。对于应用程序,我认为launch4j或其他包装器是可行的方法。

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

在unix上,如果你从shell sript(至少对于bash以及可能对于其他体面的shell)启动,你可以使用:

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.

启动java并将“goodname”作为第0个参数传递,它将在ps等中显示为进程名称。

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.

一个或许更好的替代方案(似乎也适用于top)是创建一个符号链接: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.

Windows中的快捷方式不起作用,但是windows vista / 7支持使用mklink的符号链接。这可能有用,但我还没有测试过。我不确定exec -a是否也适用于Windows上的cygwin bash。

#2


18  

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

查看launch4j,它是一个可执行的包装器,允许您分配可执行文件名。

#3


11  

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

除非您在自定义构建的可执行文件中通过JNI启动Java,否则进程名称将始终为java.exe。

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

有几个java启动器/包装器可以为您生成此可执行文件。

  • Launch4j, looks to be the most recent and up to date
  • Launch4j,看起来是最新的和最新的
  • JSmooth
  • JSmooth
  • install4J, commercial, more than you need
  • install4J,商业,超出您的需求

#4


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.

如果您正在使用Sun JDK,还可以使用“jps”命令行工具获取在该框上运行的Java进程的详细列表。

#5


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:

运行ps -ef | grep java,你应该得到一个看起来像这样的列表:

 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.

然后pkill -f peralex.MyApp会杀死正确的进程。

#6


3  

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.

这是Windows特有的。我遇到了同样的问题,我必须使用taskkill杀死特定的java程序。当我运行java程序时,tasklist显示了相同的程序,其Image名称设置为java.exe。但是使用taskkill / F java.exe杀死它将停止所有其他不需要的Java应用程序。

So I run the same java program using:

所以我运行相同的java程序使用:

start "MyProgramName" java java-program..

启动“MyProgramName”java java程序..

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:

这里start命令将打开一个新窗口并运行java程序,窗口标题设置为MyProgramName。现在来这个java程序使用以下taskkill命令:

taskkill /fi "MyProgramName"

taskkill / fi“MyProgramName”

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

您的Java程序将只被杀死。休息不受影响。

#7


2  

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

并非所有类型的exec都支持-a标志。如果你没有,argv0程序会做类似的事情。

#8


2  

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

您可以使用LD_PRELOAD垫片执行此操作:https://github.com/airlift/procname

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

当进程启动时,垫片只调用特定于Linux的prctl():

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.

调用必须在主线程上进行,因此无法从Java或甚至使用JVMTI代理执行此操作,因为这些操作发生在不同的线程上。