如何为 Java 程序设置进程名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1057247/
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 22:54:32  来源:igfitidea点击:

How can I set the process name for a Java-program?

javaprocess

提问by Mnementh

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.

如果启动了一个 Java 程序,它会在系统进程监视器中获得名称 java。许多 Java 程序很难区分。因此,如果存在一种方法,设置名称会很好,这将显示在进程监视器中。我知道这在不同的操作系统上可能会有所不同。

A simple way would be, if the java-interpreter would support a switch to set the name, like this:

一个简单的方法是,如果 java 解释器支持设置名称的开关,如下所示:

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.

但是我找不到这样的开关,所以它可能不存在。Java 中用于设置进程名称的 API 也可以。

So, so you have any suggestions?

所以,你有什么建议吗?

采纳答案by omerkudat

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 -vshows all the arguments you have passed to java.

我不知道这是否可行,但您可以使用 JDK 附带的名为“jps”的命令行工具。它就像 *nix ps,但只是 Java 程序。jps -v显示您传递给 java 的所有参数。

Also, I have seen people attach a "process name" to their java processes by adding an unused -Dmyprocessnameto the args.

另外,我看到人们通过向-Dmyprocessnameargs添加一个未使用的名称来将“进程名称”附加到他们的 Java 进程。

回答by Lloyd

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.

那是因为 Java 应用程序实际上不是可执行的,它们由 Java 虚拟机运行,这就是 java 出现在进程监视器中的原因,它是应用程序的主机。

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

然而,像 LimeWire 这样的东西,但我认为这更取决于 GCJ - http://gcc.gnu.org/java/

回答by Dave Webb

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.

如果您想使用不同的进程名称,则必须创建自己的二进制文件以使用JSmooth 之类的东西启动 Java 应用程序。

Look at this questionfor a discussion of creating such binaries.

查看此问题以讨论创建此类二进制文件。

回答by Tim Bender

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 forumyou can use C++ wrappers around the JVM executable to achieve this.

当我第一次读到这篇文章时,改变进程名称的想法让我觉得不可能。但是,根据sun 论坛上的这个古老线程,您可以在 JVM 可执行文件周围使用 C++ 包装器来实现这一点。

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.

虽然坦率地说,我想知道你真正的问题是什么,因为我猜有一个更标准的解决方案,然后尝试更改进程名称。

回答by Noel Grandin

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

你最好的选择是类似于 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

有一个错误记录在 sun bugtracker 中,但这不是高优先级 http://bugs.sun.com/view_bug.do?bug_id=6299778

回答by d.raev

as @omerkudat said:

正如@omerkudat 所说:

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 进程 {processID, params list} 如果 params 列表不足以识别您需要的应​​用程序,请尝试在运行它们时添加一些虚拟参数:

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.

并且您可以使用进程 ID 来终止/监视它。

回答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 jeffery.yuan

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

主要有两种方法:一种是已经描述过的:使用 Launch4j、WinRun4J 等工具创建本地 Windows 启动器。

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.

另一种看起来更好的方法是使用 Apache Procrun 将 java 应用程序包装为 Windows 服务。在安装服务过程中,我们可以给这个进程一个有意义的名字,比如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.

我们需要做的就是将 prunsrv.exe 重命名为 OurApp.exe,并将我们的 install|start|stop|uninstall 服务脚本中出现的每个 prunsrv.exe 替换为 MyApp.exe。

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

请参阅使用 Apache Procrun 重命名 Windows 中 Java 程序的进程名称的更多信息