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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/882826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:38:19  来源:igfitidea点击:

how to change the name of a Java application process?

javaprocessargv

提问by

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

在执行 Java 应用程序时,给它的进程名称通常是java.exejavaw.exe。但是我怎样才能让它被我的应用程序的名字调用呢?

回答by z -

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

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

回答by basszero

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 启动器/包装器可以为您生成这个可执行文件。

回答by Rob H

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 进程的详细列表。

回答by KarlP

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 javaand pass "goodname" as the 0th argument, which will be shown as the process name in psetc.

启动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 -aalso works with cygwin bash on Windows.

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

回答by Noel Grandin

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 javaand 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.MyAppwill kill the correct process.

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

回答by mpm

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

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

回答by elite21

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, tasklistwas showing the same program with Image name set as java.exe. But killing it using taskkill /F java.exewill stop all other java applications other than intended one which is not required.

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

So I run the same java program using:

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

start "MyProgramName" java java-program..

启动“MyProgramName”java 程序..

Here startcommand 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

现在使用以下 taskkill 命令杀死这个 java 程序:

taskkill /fi "MyProgramName"

taskkill /fi "MyProgramName"

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

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

回答by David Phillips

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

你可以用LD_PRELOAD垫片来做到这一点:https: //github.com/airlift/procname

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

prctl()当进程开始时,shim 只是简单地调用 Linux 特定的:

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 代理执行此操作,因为它们发生在不同的线程上。

回答by user3479450

WinRun4J

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 WinRun4Jon 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).

在许多 Java exe 包装器中,自定义 exe 只是一个启动器,该进程仍然像通常的 java(w).exe 一样运行。另一方面,在WinRun4J 中,自定义 exe 是运行您的应用程序的进程,因此例如在任务管理器中,您的自定义 exe 就是出现的内容。它支持 32 位和 64 位、控制台和无控制台(以及众多配置选项)。

回答by Benjamin James

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.

我需要在 Windows 中解决此问题,以使我能够停止特定的 Java 进程。我决定使用 .bat 文件来运行不同的 .jar 文件,.bat 文件的第一行是 TITLE“Name to give the cmd window”,然后当我查看不同的 cmd 窗口时,我可以看到这是我想通过那个 cmd 窗口停止和 Ctrl-C 的那个。