I need to run a two java programs from a batch script. I need to wait for the first java program to finish running before the second one starts. Is there a way to do this? I'm on a windows machine and I can't edit the java programs so I'm using a batch script to launch them.
So it would be something like this
cd %PARSEDIR%
set CLASSPATH=.\lib\jpcap-0.01.16.jar
set CLASSPATH=%CLASSPATH%;.\lib\jpcap.jar
START /WAIT java -Xms768m -Xmx768m -classpath %CLASSPATH% parser.test.firstStage %2
timeout 5
START /WAIT java -Xms768m -Xmx768m -classpath %CLASSPATH% parser.test.lastStage %2
I need the first program to finish before the the batch script moves on. How do I accomplish this?
Thanks!
edit: added what the code currently looks like
Use the start /WAIT command (http://ss64.com/nt/start.html)
Related
i usually use this bash script to check if my java application is running and start it again if not. I use crontab to check it
#!/bin/bash
if [ "$(pidof java)" ]
then
# process was found
echo "application running"
else
# process not found
cd /home/assist/emanager
setsid java -jar emanager-1.0.0.jar </dev/zero &>/dev/null &
fi
Now the problem is that there are other java applications running on the server, so the script does not start my app because the if condition is true. Is there a way to check if a specific java application is running?
Thanks
Your question is very similar to this one:
Start a Java application using Bat file if not already started
The main difference is you are asking for bash while the other one is in Windows. Hence replace jcmd with jps, the rest is still applicable.
I want to make job on Jenkins that starts server (MockServer on WireMock).
Server is launched from *.jar file, from terminal like that.
java -jar serverLaunch.jar
It takes over my console. To avoid that I modify this and do:
java -jar serverLaunch.jar &>/dev/null &
And that works for me on my local PC. Now I want to move it to Jenkins.
If I try to do this from "Shell command" block in Jenkins Job then:
a) java -jar serverLaunch.jar
I have task locked in queue in my Jenkins and I don't want that but server starts and works.
b) java -jar serverLaunch.jar &>/dev/null &
Job ends with success but my server is not alive.
I have wrapped this command also in .sh script and .rb script. Any idea how to make it work?
I've tried this:
https://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build
And then in Jenkins "Shell script":
daemonize -E BUILD_ID=dontKillMe /bin/bash launch.sh
But it also passes but server is not alive.
I had to check "Inject environment variables to the build process" and add:
BUILD_ID=dontKillMe
Now it is working.
Try using nohup e.g.:
nohup java -jar serverLaunch.jar &
That should prevent the process being terminated when the parent shell process exits (which I suspect is your problem).
Another effective approach would be to add a post-build action that executes a shell spawning the server.
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.
Having read the post at .bat files, nonblocking run/launch,
I still cannot achieve what I need: to have the .BAT file close once the start command is executed. My problem is that when the JVM starts the application launches a window so I end up with 2 windows being opened, when in fact one of them (the .BAT command) is just a startup process and doesn't do anything meaningful to the user.
I paste the .BAT code here:
#echo off
setlocal
rem Starts the application
rem Check for Java Home and use that if available
if not "[%JAVA_HOME%]"=="[]" goto start_app
echo. JAVA_HOME not set. Application will not run!
goto end
:start_app
echo. Using java in %JAVA_HOME%
start "Application" "%JAVA_HOME%/bin/java.exe" -jar lib/pathToMyJarFile
goto end
:end
I'd like the .BAT process to terminate (or at least the window to close) once the JVM starts.
Try javaw.exe instead of java.exe.
Use start /b, see this:
http://zeroflag.wordpress.com/2007/05/12/start-command/
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.