process isAlive() awlays return false - java

I have the following process :
Process p = Runtime.getRuntime().exec("cmd.exe /c start "
+file.getAbsolutePath() + "/script.sh");
TimeUnit.SECONDS.sleep(20);
if(p.isAlive()){
System.out.print("the process still running");
TimeUnit.SECONDS.sleep(10);
}
The problem is p.isAlive() always return false even though my process is still running (my script shell is still working).
How to fix it?

What do you think the START command does?
Start a program, command or batch script (opens in a new window.)
So, exec(...) starts a process for running cmd.exe, then the start command opens a window and starts a second process for running script.sh, then cmd.exe is done and exists, and the Process identified by p is no longer alive, since that process has ended.
Add /WAIT.
Start application and wait for it to terminate.
Process p = Runtime.getRuntime().exec(new String[] {
"cmd.exe", "/c", "start", "/wait",
file.getAbsolutePath() + "/script.sh" });

Related

How to make a process wait

I have a code that execute an external program. And now I need that my application wait the end of the execution of that external program.
But I'm not shure how I supposed to do that. I tried some things but don't work.
public Image acquireImage() throws IOException, InterruptedException {
process = Runtime.getRuntime().exec("cmd.exe /c start "+ApplicationProperties.getPath()
+ "\\.wimdesktop\\Release\\Static_GenerateGain.exe");
process.waitFor();
System.out.println("EXIT: " + process.exitValue());
return copyImage();
}
The problem is that the System.out.println("EXIT: " + process.exitValue()); print 0 but the external program still running.
You are running cmd.exe and asking it to start a process in the background. So all you are seeing is cmd.exe exit status 0 after it launches your app - and that app may still be running.
If you want waitFor to apply to the sub-process just run the exe directly without the launch wrapper:
process = Runtime.getRuntime().exec(ApplicationProperties.getPath()
+ "\\.wimdesktop\\Release\\Static_GenerateGain.exe");
Note that if your EXE depends on environment variables set by CMD.EXE then you may need to try your original command without "start" for background process:
process = Runtime.getRuntime().exec("cmd.exe /c "+ApplicationProperties.getPath()
+ "\\.wimdesktop\\Release\\Static_GenerateGain.exe");
In both cases above you may run into second issue that the command freezes, this is because you are not reading the Stdout and error streams. There are many SO posts on how to do this.

Start another java program inside a java program and kill the mother process

I use terminal command "java -jar secondApp.jar" inside my java file to start a secondApp.jar.
I need secondApp.jar to run even if first app is killed.
This scenario works perfectly in windows environment. But when I test this in linux environment(Ubuntu 16.04) it seems that killing the first process kills the both processes.
This is the code I use to start the second app.
String command = "java -jar secondApp.jar"
Process process = Runtime.getRuntime().exec(command);
What am I doing wrong?
Prepare a batch file and a linux script file with the desired java command, then try this:
if (SystemUtils.IS_OS_WINDOWS) {
// run batch file
String batchFullPath = new File("C:\\myBatchFile.bat").getAbsolutePath();
Runtime.getRuntime().exec("cmd /C start " + batchFullPath);
} else if (SystemUtils.IS_OS_LINUX) {
// run linux script
String scriptFullPath = new File("~/myScriptFile.sh").getAbsolutePath();
File workingDir = new File("~");
Runtime.getRuntime().exec("/usr/bin/xterm " + scriptFullPath, null, workingDir);
} else {
throw new RuntimeException("Unsupported Operating System");
}
(Using xterm as it is fairly safe to assume every Linux machine has it installed)

How to run a batch file of server(which runs forever) and get the control back to the java program without closing that batch

I have a java program:
Process proc = Runtime.getRuntime().exec("cmd /c callServer.bat");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
Which is calling the batch file "callServer.bat":
#echo on
cd bin
call shutdown 2011
call server.bat
This batch file is calling server.bat file which is a server batch file which should run forever during the program. I want to get the control back after starting server.bat to my java program which will do other stuff. Please help me as I have spent so much time on it and could not get any solution.
Could be as simple as running the process in a different thread. This will execute the process in a separate thread and allow your main thread to continue while the batch file runs:
Thread thread = new Thread() {
public void run() {
Process proc = Runtime.getRuntime().exec("cmd /c callServer.bat");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
}
};
thread.start();
You can call the batch file from a separate thread which can be a deamon thread so that it won't exit till all your other user threads have not finished.
hope you find this of any use
You can use the Process.exitValue() function to get the exit value without waiting for the program to close, however that obviously won't work if the program is still running.
If you wanted too you could do something like:
Process proc = Runtime.getRuntime().exec("cmd /c callServer.bat");
while(condition){
try{
exitVal = proc.exitValue();
System.out.println("Process exitValue: " + exitVal);
}
catch(Exception e){/*do nothing*/}
//other code goes here
}
Kind of ugly, but you could do that if you're not comfortable with threading. But using a thread would be the correct way to do it.

Run bat file in Java and wait

You would think that launching a bat file from Java would be an easy task but no... I have a bat file that does some sql commands for a loop of values read from a text file. It is more or less like this:
FOR /F %%x in (%CD%\listOfThings.txt) do sqlcmd -Slocalhost\MSSQL %1 %2 -d %3 -i %CD%\SQLScripts\\%%x
exit
Don't worry about the specifics they are not important. What i want is to simply run this bat file from within Java and have it wait until execution is finished. Apparently it is not easy. What i have so far is this:
Runtime.getRuntime().exec("cmd /K start SQLScriptsToRun.bat"
+" -U"+getUser()
+" -P"+getPass()
+" " + projectName);
return true;
The problem is that the exec() method returns immediately. The bat file runs for a good 2-3 minutes. I tried removing the start but to no avail. I tried many variations but it got me nowhere. Any ideas on how to do this simple task?
You should not ignore the return value of .exec(). It gives you a Process object that you can waitFor(), like this:
final Process process = Runtime.getRuntime().exec("blahblahblah");
final int exitVal = process.waitFor();
// if exitVal == 0, the command succeeded
you need to use waitFor on the process exec call returns.

cause the main process wait until another process is finished

I have a synchronization problem in java.
I want my main thread to wait until process "p1" is finished.
I have used "waitfor" method. it has not worked for me.
Process p1 = runtime.exec("cmd /c start /MIN " + path + "aBatchFile.bat" );
p1.waitFor();
Could anybody help me please?
Thank you so much.
The problem here is that the Process object you get back from exec() represents the instance of cmd.exe that you start. Your instance of cmd.exe does one thing: it starts a batch file and then exits (without waiting for the batch file, because that's what the start command does). At that point, your waitFor() returns.
To avoid this problem, you should be able to run the batch file directly:
Process p1 = runtime.exec(path + "aBatchFile.bat");
p1.waitFor();
Alternately, try the /wait command line option:
Process p1 = runtime.exec("cmd /c start /wait /MIN " + path + "aBatchFile.bat" );
p1.waitFor();

Categories