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
Related
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
I was able to use an amalgamation of SO questions to get a line of code that would:
Run a batch file from within a Java application
Include an argument located in the same directory as the batch file
The argument & batch file are in a directory unrelated to the JAR/java app
So I used this:
Runtime.getRuntime().exec("cmd /c start batchfile.bat argument.js", null, new File( path ));
This works well except that it leaves the cmd prompt open after it has finished executing the batch file. I've looked around and it seems like having the /c flag should make it close after running, but for me it has been staying open. I tried removing 'start' but this meant that the cmd prompt never opened up at all.
Is it because I'm combining having an argument and using a different path that it's not causing the cmd prompt to open and then close after completion like most examples on SO?
Calling both cmd.exe and start in that order is creating a separate window but it doesn't seem like the command shell is terminating based on what you described. I was able to replicate this behavior in a simple batch script. If you want to leave your Java call as it is, check to make sure your batch script includes an exit statement. Preferably exit based on a success or failure condition within your batch script (i.e.: exit 0 or exit 1, etc...)
:: batchfile.bat
...
exit 0
Assuming you are not firing up any new shells within your batch script or prompting for input, adding an explicit exit to your existing batch script should cause the window to close. You can also try to leave your batch script as is, and as others have suggested in comments, change the order of your command text slightly.
Use this instead
start cmd.exe /c ...
Where you call start first and then call cmd.exe /c after followed by your parameters as needed. I tried both options here and either worked fine to cause the batch window to close as expected.
Pass the below command to Runtime to execute.
taskkill /F /IM (processName)
Runtime.getRuntime().exec("taskkill /F /IM "+pProcessName);
In my Java application, I create a temporary batch file which does some work for me and the self deletes using this line del /f /q %0. The bat file is launched using ProcessBuilder and then System.exit(0) is called.
This workflow works perfectly, however once the batch file reaches the end I am left with a command prompt window saying "The batch file cannot be found" and waiting for another command. Is there any way I can stop this from happening?
del /f /q %0 &exit
should help
I am invoking a java swing program from command program and after executing the java program the console remains open. My bat file contains :
set Gantt_Generator_v5.0="C:\Program Files\GANTT_GENERATOR_V5.0"
java -jar %Gantt_Generator_v5.0%\GANTT_GENERATOR_V5.0.jar "C:\Program Files\GANTT_GENERATOR_V5.0\test\Parameter_File.csv" FALSE
exit
When I close the command prompt the java application quits.
Tried options : changed the extension from .bat to .cmd but didn't work for me.
Any other solutions?
used "START JAVAW..." so the final Script is :
set Gantt_Generator_v5.0="C:\Program Files (x86)\GANTT_GENERATOR_V5.0"
start javaw -jar %Gantt_Generator_v5.0%\GANTT_GENERATOR_V5.0.jar "C:\Program Files (x86)\GANTT_GENERATOR_V5.0\test\Parameter_File.csv" FALSE
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.