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

发布于 2024-07-25 21:12:32 字数 323 浏览 5 评论 0原文

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

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

java -processname MyProgram -jar MyProgram

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

那么,您有什么建议吗?

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.

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

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.

So, so you have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

路弥 2024-08-01 21:12:32

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

另外,我还看到人们通过向参数添加未使用的 -Dmyprocessname 来将“进程名称”附加到他们的 java 进程。

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

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

禾厶谷欠 2024-08-01 21:12:32

正如@omerkudat所说:

jps -v

打印出所有java进程{processID,params list}
如果参数列表不足以识别您需要的应​​用程序,
尝试在运行时添加一些虚拟参数:

java -Dname=myApp -cp  myApp.jar some.client.main.MainFrame

这将打印如下:

7780 MainFrame -Dname=myApp

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

as @omerkudat said:

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

请爱~陌生人 2024-08-01 21:12:32

您可以使用 LD_PRELOAD 填充程序来执行此操作:https://github.com/airlift/procname

当进程启动时,填充程序只是调用 Linux 特定的 prctl()

static void __attribute__ ((constructor)) procname_init()
{
   prctl(PR_SET_NAME, "myname");
}

该调用必须发生在主线程上,因此不可能从 Java 甚至其他线程中执行此操作使用 JVMTI 代理,因为它们发生在不同的线程上。

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.

飘落散花 2024-08-01 21:12:32

当我第一次读到这篇文章时,我觉得更改进程名称的想法是不可能的。 然而,根据太阳论坛上的这个古老帖子,您可以使用 JVM 可执行文件的 C++ 包装器来实现此目的。

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

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

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.

┾廆蒐ゝ 2024-08-01 21:12:32

你最好的选择是像 launch4j
http://launch4j.sourceforge.net/

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

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

There is a bug logged in the sun bugtracker for this, but it's not high priority
https://bugs.java.com/bugdatabase/view_bug?bug_id=6299778

雄赳赳气昂昂 2024-08-01 21:12:32

主要有两种方法:一种是如上所述:使用 Launch4j、WinRun4J 等工具创建本机 Windows 启动器。

另一种似乎更好的方法是使用 Apache Procrun 将 java 应用程序包装为 Windows 服务。 在安装服务过程中,我们可以给该进程起一个有意义的名称,例如OurApp.exe。

我们需要做的就是将 prunsrv.exe 重命名为 OurApp.exe,并将安装|启动|停止|卸载服务脚本中出现的所有 prunsrv.exe 替换为 MyApp.exe。

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

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

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.

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.

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

◇流星雨 2024-08-01 21:12:32

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

看看这个问题关于创建此类二进制文件的讨论。

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.

Look at this question for a discussion of creating such binaries.

心碎无痕… 2024-08-01 21:12:32

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

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文