-1

I am looking for a solution to figure out if my .jar is already running. However, I need this to be able to work on Windows, RedHat, Solaris, OEL, etc. A solution in perl would be great. However, I would also be okay trying a solution where the jar is called and then in the main class checks to see if another jar is already running (and if it is, it shutsdown). I am calling it using java -jar

I looked into the following: How can I check if a Unix process is running in Perl? However it did not seem to work for Windows.

I also looked at this post: Using Perl, how do I check if a process with given name is running or not? However this would only work for Windows.

I suppose it would be possible to use a combination of the two after figuring out which OS the jar is running on, however I am looking for a 'cleaner' option.

Any help would be great.

4

3 回答 3

4

使用 java 进程状态工具:jpsjdk 自带的。它列出了正在运行的程序,如果启动了某个程序java -jar start.jar,它将显示 jar 文件的名称:

root@MYBOX:/opt/jetty/logs# jps
31557 start.jar
1891 Jps

您可以构建您的脚本jps以查看您的 jar 是否已经在运行

您可以提供其他选项jps来打印更多或更少的数据:http ://docs.oracle.com/javase/1.5.0/docs/tooldocs/share/jps.html

于 2013-08-09T13:27:24.920 回答
3

一个简单的解决方案是将程序的状态写入文本文件或数据库中,并在您想知道应用程序的状态时读取它。

正如 George 提到的,如果没有任何内容写入,例如,每秒写入文本文件(例如),您可以轻松查看程序是否卡住,比如说最后 5 秒。

此外,文本文件是独立于操作系统的:-)

于 2013-08-09T13:28:12.510 回答
0

Java 可能直接运行jar( java -jar foo.jar),或者可能是 jar 包含您正在运行的 Java 程序所需的类。

第一个很容易。您可以使用jps它将显示所有正在运行的 Java 命令。

第二个更难。当您执行 Java 程序时,您甚至无法依赖$CLASSPATH或查看参数的值。-cp该 jar 可能会在该类路径中提及,但不是必需的,或者该 jar 在META-INF/MANIFEST.MF嵌入在您正在使用的一个 jar 中的文件的类路径中提及。

于 2013-08-09T13:47:33.190 回答