[英]Start a Java application using Bat file if not already started
I want to check using a bat file if a Java program is already running. 我想使用bat文件检查Java程序是否已在运行。 If its not running then start it using
start javaw
. 如果它没有运行,则使用
start javaw
启动它。
I have tried WMIC
and I am only successful so far to get the PID. 我已经尝试过
WMIC
,但到目前为止,我仅成功获得PID。
WMIC process where "CommandLine like '%MyServer.jar%' and Name like '%javaw%'" get ProcessId
But how to write an if else condition in the bat file to start? 但是,如何在bat文件中编写if else条件来启动呢?
I tried using tasklist and find 我尝试使用任务列表并找到
tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /N "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" echo Programm is running
But for all the java applications myapp.exe
will be java.exe
. 但对于所有Java应用程序,
myapp.exe
将是java.exe
。
Please help... 请帮忙...
PS: There are options for this to be done via the Java program itself, but my specific situation asks for a bat file solution. PS:有一些选项可以通过Java程序本身来完成,但是我的具体情况要求使用bat文件解决方案。
You can use jcmd.exe to see the main class of all the java processes running. 您可以使用jcmd.exe查看正在运行的所有Java进程的主类。 The IF-ELSE syntax of the batch file is explained in the following thread
以下线程说明了批处理文件的IF-ELSE语法
How to use if - else structure in a batch file? 如何使用if-批处理文件中的else结构?
jcmd | find /I /N "sun.tools.jconsole.JConsole"
IF "%ERRORLEVEL%" GTR "0" (
jconsole.exe
)
IF %ERRORLEVEL% EQU 0 (
echo Programm is running
)
This link would help with the IF construct - http://ss64.com/nt/if.html 该链接将有助于IF构造-http: //ss64.com/nt/if.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.