简体   繁体   English

如果尚未启动,则使用Bat文件启动Java应用程序

[英]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.

相关问题 Window bat 脚本启动 java 程序加载 dll 失败,但使用 IntelliJ IDEA 成功启动? - Window bat script start java program loading dll failed, but successfully started using IntelliJ idea? 创建Java应用程序的.bat文件 - Creation of a .bat file of a java application 如果通过双击 JNLP 文件启动,如何允许 JAVA Web 启动应用程序访问 macOS Catalina 上的文件系统? - How to allow JAVA Web Start application to access file system on macOS Catalina if it is started by double clicking on JNLP file? 通过bat文件执行java应用程序停止 - Executing java application through a bat file stops bat文件无法启动,并且bat文件位于文件夹中 - bat file does not start and bat file is in a folder 用bat文件启动视频播放器,java窗口中出现失败消息 - start video player with bat file, fail message in the java window 使用Java从BAT文件获取输出 - Get output from BAT file using Java 尝试以.bat文件启动两个.bat文件 - Trying to start two .bat files with a .bat file 无法使用Windows bat文件启动kafka connect分发 - Cannot start kafka connect distributed using windows bat file 正在将Java Web应用程序的蝙蝠文件作为Windows服务运行并给出错误 - Running bat file for java web application as Windows Service giving error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM