I am working with one cmd like executable and source code of that is not available.
Executable is having multiple command sequence to be entered before it exit. Each command takes random time from 1 sec to 1 min.
I tried with Java's Process builder and runtime but I am getting output only after program completely exits from execution. Also, I am not able to insert any command to that executable via BufferedWriter. So my question is how to get output of executable while it is still running
Quenstions referred: Executing an EXE from Java and getting input and output from EXE
java runtime.getruntime() getting output from executing a command line program etc.
I have simple Java Class with a main function. I run this class with command-line 'command' which is present in the bat file, I have scheduled the bat file to run after every 2 hours.
This .class uses some resources which some time are not available at the request and due to timeout the .class terminated abnormally.
What I want to is if the bat terminates abnormally is there a way to check it and run it again until it runs properly.
Secondly if the bat terminates abnormally, is there a way to roll back the operations this .class performed before terminating. Lets say it was updating some text in a text file. Is there a way to roll it back.
There are many causes of abnormal termination.
There is a heuristic that you can adopt here:
an exception finding its way up to the main thread and which is not caught causes the JVM to terminate with an exit code of 1;
by exception above, this includes such abnormalities as OutOfMemoryError; understand that by "an exception" in the statement above, it is "any instance of Throwable"; Error is a subclass of Throwable, OutOfMemoryError is a subclass of Error;
if you wish for your program to terminate for other anomalous conditions, use System.exit() and ensure that the error code is something other than 1 because of the above.
Secondly if the bat terminates abnormally, is there a way to roll back the operations this .class performed before terminating. Lets say it was updating some text in a text file. Is there a way to roll it back.
No generic way; this really depends on your program itself. If you are talking about file I/O, there exist several paradigms to ensure that the original contents of files do not end up corrupted, at least as far as the programming language itself is concerned; more severe corruption scenarios are delegated to the filesystem itself.
Side note about exit codes... At least with Unix systems, you should be wary that in fact "valid" exit codes range from 0 to 127; yes, that is 7 bits. The 8th bit is there to signal that the process termination is due to a signal, in which case the lower 7 bits are to be interpreted (understand, "read") as the signal number which triggered program termination.
If you want to have a .bat run continuously until it runs properly after crashing you can achieve this by creating a windows service. When you create the windows service you can specify what happens after crashing, how long to wait before running the script again, and how many times it should try to run the script again.
As far as reverting what a script has done to a textfile, there are multiple solutions:
Have your script make a copy of the file, you can rename it original_new, and manipulate the new copy. At the end of the script you can have it delete the original and replace it with the copy by changing original_new to original. In case the program crashes, you will have two files, so at the beginning of your script, check to see if original_new already exists, if it does, delete it then copy the original.
You can keep a record log of everything the script does and on the startup of the script it checks the log and if the log is incomplete then the script first reverts any of the logs until it reaches the last complete sign and then runs the actual intension of the script.
It sounds like the bat fails because the java class job fails. If failure of the java job can be detected inside the batch file, you should be able to do everything you want from there.
First, to confirm the java job raises an error the batch identifies, add the following after the line that runs the java class job and test it. You should see errorlevel 0 with success and >0 with error:
REM THIS IS LINE THAT RUNS JAVA JOB
echo errorlevel is "%errorlevel%
pause
Assuming the above returns accurate exit info, here's some code that implements the other answers and reruns the java job.
To continuously rertry the java job until it runs successfully, add a label and a goto statement:
REM Add following label to a line before the java class job
:runhere
REM THIS IS LINE THAT RUNS JAVA JOB
rem Check errorlevel and retry if failure
If not %errorlevel%==0 goto runhere
Exit
To rertry the job n times and then quit even if not yet successful, create backup file as #niebloomj suggests and set a counter. This tries to run the job 10 times.
set COPYCMD=/Y
set Outputfile=path\file.ext
set Outputbak=path\file_bak.ext
copy "%Outputfile%" "%Outputbak%"
Set /a count=0
:runhere
REM THIS IS LINE THAT RUNS JAVA JOB
If %errorlevel%==0 (
:: now have new good output file
del "%Outputbak%"
exit
)
rem Check count and retry
If %count% LSS 10 (
set /a count+=1
goto runhere
)
rem When count = the "check" number above, replace old file and quit
copy "%Outputbak%" "%Outputfile%"
del "%Outputbak%"
echo Java job failed after retries. "%Outputfile%" not updated.
pause
Exit
I have made shortcuts within my file directory to run the startNetworkServer and my jar file. These are two serparate shortcuts that I then run within a single batch file. My batch file starts the network server, then pings 1.1.1.1 with a wait of 5 seconds before then running the jar file, hence opening my java GUI.
My issue is, I wish to stop the network server again upon closing my java GUI program. To do this I know I can run the stopNetworkServer command, however, placing this in the batch file after the starting of my jar file results in the network server stopping before or whilst the jar file is running. I want it to stop upon exiting my java program.
Is there anyway I can check to see if the jar file is open? Or tell it to wait until the jar file is closed? Or even return something in my java code to kick start this?
J
Fixed, just had to add a /W prefix before running my Jar file link. This means that the next process will not start until that one finishes.
The final batch file (.bat) looks like this:
START startNetworkServer.lnk
PING 1.1.1.1 -n 1 -w 5000 >NUL
START /W Run.exe.lnk
START stopNetworkServer.lnk
J
I have to run a bat file in a loop in my java code. it means when a process finish for a file another process start after that. I ma using Thread.sleep in my project after calling process bat file. but the problem is I don't know how a bat file does it take, for some file it takes 1 hour for some of them takes 10 minutes. how can i understand when a process will be finished ?
If you run it by creating a Process with Runtime.getRuntime().exec(), you can call the waitFor() method on the Process to wait for it to exit.
Using Process.exitValue() you can even read error levels that the .bat files might return.
I have a java project built that I want to run from another java program. But when I call it using the exec command it does not execute immediately but waits for the current program to end. If I add a waitFor statement then the Program hangs as the main program waits for the process and the Process is waiting for the Main program. Does anyone know how I can solve this? Or why it is behaving in such a manner? I need this jar file to execute before a second one can.
Runtime.getRuntime().exec("java -jar \"JavaProject1/dist/JavaProject1.jar\"");
System.out.println("Hello");
p.waitFor();
The location of the jar file is fine and it prints the hello. The main class for the jar file I want to run is in this thread at DaniWeb
Amongst other things, you have to keep reading from the processes STDOUT and STDERR, otherwise it will block.
See, for example, http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
why it is behaving in such a manner?
Because You are creating deadlock in your own program.
When you add waitFor() your program main() in this case waits for the external process to get finished.
It returns Zero when the external process gets fnished normally.
May be the external program has some issues