I have run an octave script from Java using
ProcessBuilder pb = new ProcessBuilder("./ProcessImg");
Process p = pb.start();
But after executing these statements, the program isn't waiting for the process to finish and comeback.
How do I make the program wait for it? I want to use the file spit out by that script.
Currently I am running the program twice, so that first time it creates the files and also an exception,the second time the program uses the previously created file.
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 a batch file which is part of an installer I have made. I would like the file to execute then when the file has finished to continue with the rest of the installation (which depends on the output of the batch file so they cannot be done in parallel). I am using getRuntime().exec() to run the batch file and then using process.waitFor() to wait for completion. When using the "/C" option process.waitFor() fires immediately even though the command is still running. When using the "/K" option process.waitFor() never returns, even when I have exit at the bottom of the file to close the window.
Is there another way to wait for a program to finish executing or will I have to do something hacky with the batch file to signal the application somehow that it is complete?
I'm running a Java (.jar) program from a bat script that near the end has the following (on windows)
Runtime.getRuntime().exec("svn co http://myrepository.com/someproj");
Thread.sleep(20000);
It checks out about 1/10th of the directories and 1 file and then it does nothing. I suspect the reason it is pausing on the file but I couldn't find anything. The bat script is run as an administrator.
A common reason for an exec command to block is that it is either waiting for input from the parent process (or the console), or blocked because the parent process is not reading the child processes standard output or error stream.
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