Exec Command doesnt seem to be executing properly in java - java

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

Related

Java runtime issue from Swing application

I have one swing application from which I am executing one jar file, which will do some processing internally. Process I have is as below:
1. one java file with main() which loads swing GUI. From that GUI I can browse and load required jar files to execute.
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
MigrationProcessElementDialog.createAndShowGUI();
}
});
}
From swing application I am loading jar file as:
Runtime rt = Runtime.getRuntime();
// replacePath is the path of the jar file to be loaded.
Process proc = rt.exec("java -jar " + replacePath);
int exitVal = proc.waitFor();
When I trigger execution, from Task Manager I am seeing, two javaw.exe (one for eclipse, one for SWING GUI) and one java.exe for program flow.
But program flow continues few times (evident from log update) but stuck after certain time.
As soon as, I kill my swing GUI javaw.exe; program flow starts and continues rest of the part promptly. So it seems to me that somehow javaw.exe is blocking java.exe execution. Is it at all possible? what's the resolution of it?
If I execute, my process executable jar from command prompt using normal java -jar "path" command, program flow is not stuck, it's working as expected.
Only facing the problem when I am executing from GUI or using Runtime. I used ProcessBuilder also; but faced same problem.
If any one can please give me any clue it will be really helpful. Thanks!
You should handle output of process (both of stdout, stderr ). Because, these outputs will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream() ). If not handled, it will block when child process produces output.
Process documentation
Some native platforms only provide
limited buffer size for standard input and output streams, failure to
promptly write the input stream or read the output stream of the
subprocess may cause the subprocess to block, and even deadlock.

Runtime exec pauses half way through execution

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.

How can I undrestand a process will be finished in java programming

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.

Powershell process hanging when called from Java application

I am trying to write a simple application that takes in a command line arguement (which will be a Powershell ps1 file) and then run it. So I have experemented with a number of different approaches and seem to be running into a problem. If I attempt to invoke powershell from within java, the windows process is started and is visible via process explorer, however powershell never returns, it hangs in some sort of loop by the looks of it. The command I am using is:
String command = "powershell -noprofile -noninteractive \"&C:\\new\\tst.ps1\"";
The command is then executed using:
Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);
At the moment I am hard coding the location to the ps1 file as I was trying to rule this out as an issue. Using a process explorer I can see the hanging powershell process and the command that was passed to it was :
powershell -noprofile -noninteractive "&C:\new\tst.ps1"
which when copied into a cmd window, works to launch the tst.ps1 file. The file itself is incredibly simple in this example and I think I can rule it out being the cause of the freeze as I have tried to launch other ps1 files the same behaviour can be seen.
To further add to the confusion, if I use the java code posted above and pass in powershell commands instead of a file name then it successfully runs.
I've scoured the web and see lots of people experiencing the same issue but no one seems to have posted there solution, I hope its a simple oversight on my part and can be easily fixed.
Any hints/tips are appreciated :D
Alan
You have to close OutputStream in order for Powershell to exit.
Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);
proc.getOutputStream().close();
Is your external program writing to the standard outputs (err and out)?
If yes, it can hang waiting for you to consume them from the java parent process.
You can get those as InputStreams by calling
Process.getInputStream()
and
Process.getErrorStream()
There's more details here:
Javadoc for Process

exec command in java

If i am using following command in java:
Process ps = Runtime.getRuntime().exec("some .exe file of VB");
How do I know that the particular .exe has done its job eg: it executed successfully.
How do i know that it has some error or just completed half task in java.
How should I design my program in java to know or Is there any way to tell java from VB.
Any help is appreciated.
I would assume that you could look at the exit status of the program: ps.exitValue() or you could read the stdout/stderr ps.getInputStream() / ps.getErrorStream() respectively.
You get back a Process
http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/lang/Process.html
Which has such methods as:
exitValue()
getErrorStream()
waitFor()
Which will get you what you need

Categories