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?
Related
I am making a program to automatically backup a MySQL database from within java using MysqlDump. I want to get the file created and zip it. However, I am having an issue with the MySQLDump.
I am using the following to create a MySQLDump:
Process pr = Runtime.getRuntime().exec(
"mysqldump -u "+user+" --password='"+password+"' "+database+" > /root/moltres/backups/sql/"+database+".sql"
);
I have a while loop after this which remains in the loop if pr.isAlive(). I thought this would work, but it appears that when the command is executed, the process instantly becomes no longer alive. I could make the thread sleep, but for how long? How can I make the same SQL backup, but detect when the mysqldump command has completed?
The command is exiting immediately all right. It is failing, because the redirection isn't being understood by mysqldump.
This is not how you use Process. You need to:
Use ProcessBuilder.
Merge the output and error streams.
Add sh -c or cmd /c as appropriate to the beginning of the command line to handle the redirection.
Start the process.
Consume the output stream and log it, reading until end of stream.
Call Process.waitFor().
Get the process's exit code and log that.
Why you're using Java for this at all is a mystery. It's just a shell script. Indeed you can get MySQL to schedule its own backups automatically, without even needing a script.
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'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 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