Start a Java application using Bat file if not already started - java

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.
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
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
But for all the java applications myapp.exe will be java.exe.
Please help...
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.

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
How to use if - else structure in a batch file?
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

Related

Batch script with Javaw

I'm trying to do a script in batch to launch many java applications.
I did a loop with a .txt file, I'm trying to start a programm which doesn't open many windows, but just one and print all the outpout of the others. So I did that :
start java gateway/gateway %%a
Of course th programm is running in background , but how can I redirect the outpout to the actual terminal ?
Thanks
Try this to redirect programs output back to the Cmd window:
Start "" /B java gateway/gateway %%a

Batch File Error When Setting CLASSPATH

I have a number of weka datasets I need to run at once so I was creating a batch file to do that.
#ECHO OFF
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI
SET CLASSPATH=C:\Program Files (x86)\Weka-3-6\weka.jar
java weka.classifiers.functions.LinearRegression -t %%~nI -x 10
)
When I run the SET CLASSPATH command and the java command in a regular command line they work fine, and they also work on their own in a batch file but as soon as I nested them in a for loop, I started getting "\Weka-2-6\weka.jar was unexpected at this time" errors.
I'm not an expert in Java or in Batch file programming so forgive me if the fix is really simple but I've been up and down the internet and I haven't found any solutions to this problem. What am I doing wrong here?
Thanks for your help.
#ECHO OFF
setlocal
SET "CLASSPATH="C:\Program Files (x86)\Weka-3-6\weka.jar""
FOR /r %%I IN (*.arff) DO (
ECHO Running %%~nI
java weka.classifiers.functions.LinearRegression -t %%~nI -x 10
)
endlcoal
set class path can be expanded after the for loop is finished.So better defined it outside the loop.

Get exit code from a java application in batch file

I'm currently making an effort to create test cases for one of our java applications.
In my code, my java application calls a batch file, which in turn starts a separate java process, that returns an error code that I need to consume from the calling java application.
I'm doing the following to invoke my batch file:
Process process = runTime.exec(new String[]{"cmd.exe","/c",scriptPath});
exitValue = process.waitFor();
The batch file is as follows:
#echo off
cd %~dp0
java -cp frames.FrameDriver
SET exitcode=%errorlevel%
exit /B %exitcode%
Now with the above code and batch file, my JUnit framework just hangs on this particular test case, as if it's waiting for it to end. Now when JUnit is hanging on the test case, going to the Task Manager, and ending the java.exe process would allow the JUnit framework to continue with the other cases.
Running the .bat file by double clicking it runs the Java application normally.
Adding the START batch command before the java command in my batch file seems to fix the hanging problem, but I can't seem to get the correct exit code from my Java application as it's always 0. (The Java application exits with an error code using System.exit(INTEGER_VALUE)). I'm assuming that the %errorlevel% value is being overwritten by the "start" command's own exit value.
Can anyone please tell me how to solve this problem?
Thanks.
P.S: If it makes any difference, I'm using JDK 5 and Netbeans 5.5.1.
Don't use the /B on your exit. Here is how I would do a script:
#ECHO off
ECHO Running %~nx0 in %~dp0
CALL :myfunction World
java.exe -cp frames.FrameDriver
IF NOT ERRORLEVEL 0 (
SET exitcode=1
) ELSE (
SET exitcode=0
)
GOTO :END
:myfunction
ECHO Hello %~1
EXIT /B 0
:END
EXIT %exitcode%
NOTE: Also, you can execute java program in 3 different ways:
java.exe -cp frames.FrameDriver
CALL java.exe -cp frames.FrameDriver
cmd.exe /c java.exe -cp frames.FrameDriver
This is very critical since, your Java command may exit with a exit code and in order to pass the exit code correctly to the ERRORLEVEL var, you need to use the correct method above, which I am unsure about.

Starting and killing java app with shell script (Debian)

I'm new to UNIX. I want to start my java app with a script like so:
#!/bin/sh
java -jar /usr/ScriptCheck.jar &
echo $! > /var/run/ScriptCheck.pid
This is supposedly working. It does run the app and it does write the pid file. But when I try to stop the process with a different script which contains this:
#!/bin/sh
kill -9 /var/run/ScriptCheck.pid
the console gives me this error:
bash: kill: /var/run/ScriptCheck.pid: arguments must be process or job IDs
My best guess is that I'm not writing the right code in the stop script, maybe not giving the right command to open the .pid file.
Any help will be very appreciated.
You're passing a file name as an argument to kill when it expects a (proces id) number, so just read the process id from that file and pass it to kill:
#!/bin/sh
PID=$(cat /var/run/ScriptCheck.pid)
kill -9 $PID
A quick and dirty method would be :
kill -9 $(cat /var/run/ScriptCheck.pid)
Your syntax is wrong, kill takes a process id, not a file. You also should not be using kill -9 unless you absolutely know what you are doing.
kill $(cat /var/run/ScriptCheck.pid)
or
xargs kill </var/run/ScriptCheck.pid
I think you need to read in the contents of the ScriptCheck.pid file (which I'm assuming has only one entry with the PID of the process in the first row).
#!/bin/sh
procID=0;
while read line
do
procID="$line";
done </var/run/ScriptCheck.pid
kill -9 procID
I've never had to create my own pid; your question was interesting.
Here is a bash code snippet I found:
#!/bin/bash
PROGRAM=/path/to/myprog
$PROGRAM &
PID=$!
echo $PID > /path/to/pid/file.pid
You would have to have root privileges to put your file.pid into /var/run --referenced by a lot of articles -- which is why daemons have root privileges.
In this case, you need to put your pid some agreed upon place, known to your start and stop scripts. You can use the fact a pid file exists, for example, not to allow a second identical process to run.
The $PROGRAM & puts the script into background "batch" mode.
If you want the program to hang around after your script exits, I suggest launching it with nohup, which means the program won't die, when your script logs out.
I just checked. The PID is returned with a nohup.

Running a batch file from java

I just downloaded MCP to see how things work behind the scenes in Minecraft.
Inside MCP there are a bunch of batch files that you use to do things like: decompile, recompile, startclient, etc.
What I would like to be able to do is run these batch files from a basic java gui.
I'm good with the gui part but I havent got a clue how to run those batch files.
Here is an example of one of the batch files:
The file is at:
C:\MCP\startclient.bat
startclient contains the following:
#echo off
:try_python
set PYTHON=python
%PYTHON% --version >NUL 2>NUL
if errorlevel 1 goto try_python_mcp
goto foundit
:try_python_mcp
set PYTHON=runtime\bin\python\python_mcp
%PYTHON% --version >NUL 2>NUL
if errorlevel 1 (
echo Unable to locate python.
pause
exit /b
)
:foundit
%PYTHON% runtime\startclient.py conf\mcp.cfg
pause
Can it be done?
You can easily run a batch file from java using Runtime:
Process p = Runtime.getRuntime().exec("cmd /c start " + yourbatchFileName);
you can also grab I/O of the process, using p.getOutputStream(), p.getInputStream() etc.
See more about the Process class here.
And I suggest you take a look at this article as well.

Categories