如果尚未启动,则使用Bat文件启动Java应用程序 [英] Start a Java application using Bat file if not already started

查看:80
本文介绍了如果尚未启动,则使用Bat文件启动Java应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用bat文件检查Java程序是否已在运行.如果未运行,则使用 start javaw 启动它.

I want to check using a bat file if a Java program is already running. If its not running then start it using start javaw.

我已经尝试过 WMIC ,但到目前为止我只能成功获得PID.

I have tried WMIC and I am only successful so far to get the PID.

WMIC process where "CommandLine like '%MyServer.jar%' and Name like '%javaw%'" get ProcessId

但是如何在bat文件中写入if else条件以启动呢?

But how to write an if else condition in the bat file to start?

我尝试使用任务列表并找到

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

但是对于所有Java应用程序, myapp.exe 将是 java.exe .

But for all the java applications myapp.exe will be java.exe.

请帮助...

P.S:有一些选项可以通过Java程序本身来完成,但是我的具体情况要求使用bat文件解决方案.

P.S: There are options for this to be done via the Java program itself, but my specific situation asks for a bat file solution.

推荐答案

您可以使用jcmd.exe查看正在运行的所有Java进程的主类.下列线程中说明了批处理文件的IF-ELSE语法

You can use jcmd.exe to see the main class of all the java processes running. The IF-ELSE syntax of the batch file is explained in the following thread

如何使用if-else结构批处理文件?

jcmd | find /I /N "sun.tools.jconsole.JConsole"


IF "%ERRORLEVEL%" GTR "0" (
    jconsole.exe
)

IF %ERRORLEVEL% EQU 0 (
    echo Programm is running 
) 

此链接将有助于IF构造- http://ss64.com/nt/if.html

This link would help with the IF construct - http://ss64.com/nt/if.html

这篇关于如果尚未启动,则使用Bat文件启动Java应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆