how to get process end status in java? - java

I have run an external tool through exce() function in a separate command line console.
command = "cmd.exe /c start /min doxygen " + strDoxyfilePath;
System.out.println("command : " + command);
//pass the command to execute
Process p=Runtime.getRuntime().exec(command);
I used this for read input stream:
BufferedReader br = new BufferedReader(new InputStreamReader
(p.getInputStream(), "UTF-8")); //read output of doxygen
String line = null;
while ((line = br.readLine()) != null){
System.out.println("I M HERE: "+line);
}
But control doesn't go inside while loop and I want to get proper signal at the process end.

I think it is because of the start in your command. You are probably doing it to avoid a cmd window but I think you cannot not interact with the program then.
Try
command = "cmd.exe /c doxygen " + strDoxyfilePath;
Also, note that
You also need to read the stderr (p.getErrrorStream())
Runtime.exec is not a great way to start a child process. ProcessBuilder is the newer and better way to do it.

I think the classic When Runtime.exec() won't still explains it best.

Related

Linux: Running Transmission through Java Runtime.exec() gives error

I have a bit of a weird situation. I'm writing a Java program to communicate automatically with Transmission on my Raspberry Pi (Linux).
I have the following code (simplified and edited for convenience/security):
String s;
String fullCommand = "transmission-remote -n username:password -a /location/to/file.torrent";
String[] cmd = fullCommand.split(" ");
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((s = br.readLine()) != null) {
System.out.println("line: " + s);
}
p.waitFor();
System.out.println("exit: " + p.exitValue());
p.destroy();
The output I get is "non-existing or invalid torrent file".
If I run the exact same script on the command line, I get success (so the file exists and is valid).
I've read that Runtime.exec() has problems with spaces. Hence the split(" ").
It does not work without it either.
I know Runtime.exec() isn't the same as command line, but is there any way I could get this working?
I have a workaround, but I'd like to do it in one step.
If anyone is interested: the workaround is writing the command to a .sh file, make it executable and run that using Runtime.exec(). This does work.

Cannot start Steam.exe from Runtime.getRuntime().exec(..)

I have been trying to start Steam from inside my Java program.
I have tried this:
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec("cmd.exe start \"\" \"C:/Program Files (x86)/Steam/Steam.exe\" -login myid mypassword"); //the string amounts to start "" "C:\Program Files (x86)\Steam\Steam.exe" -login myid mypassword
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
System.out.println(output);
This would do nothing. I don't even get any error. My mouse becomes a loading mouse as if something is loading in the background. But actually nothing happens.
Although, when I try to run commands such as taskkill /F /IM Steam.exe, they work perfectly fine and actually I see Steam closing.
I was able to run it by removing start.
Runtime.getRuntime().exec("cmd.exe \"C:/Program Files (x86)/Steam/Steam.exe\" -login myid mypassword");
Even removing cmd.exe worked as well
Runtime.getRuntime().exec("\"C:/Program Files (x86)/Steam/Steam.exe\" -login myid mypassword");
But I ran into a very little problem, which I will ask some other time. That little problem is that the process won't execute the next instruction unless I close the Steam. I tried to run it with cmd.exe /c flag, but that didn't work either.

Java run Process unable to get standard out

I have my code below. I am trying to run my ruby script and get the standard out, I don't really need standard error in this case. For some reason, I could not get any output at all in my production host, and I only get empty string. The same code works fine in my desktop. If I run my ruby script myself, I could get output from it. My ruby script only has a puts statement for now. Any one know the reason? Is it that I need to some kind of block to wait for the ruby script to finish?
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("ruby", "tests.rb");
Process process = processBuilder.start();
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder output = new StringBuilder();
String line;
while ((line = bufferReader.readLine()) != null) {
System.out.println("Line is: " + line);
output.append(line + "\n");
}
bufferReader.close();
return output.toString();
I have redhat machine running zsh.
You need to close the process's output stream (its standard input), and you should probably merge the output and error streams. You might think you don't want them both but you might be wrong, and if the process produces enough unread error output it will stall.

start a process in a cmd window and get output

I want to start a process in a CMD window with Java, and the easiest way to do that is by
Runtime.getRuntime().exec("cmd /c start program.exe")
The problem is that now I can't get the input from the process. How can I get the output from the process and be able to run it in a separate CMD window?
Your problem is that start is a separate command whose purpose is to launch a completely new process unrelated to the cmd that invokes start. Whatever start then executes is not connected to the original cmd and cannot be accessed by your Java program.
If you need to access the subprocess' in/out/err streams, do not use start.
Hey bro if you want to println the output process of your process use this
Process process= Runtime.getRuntime().exec("cmd /c start program.exe");
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
line = br.readLine();
System.out.println(line);
}
with this you will get each output process exactly the same with cmd output.
if you want to process 2 cmd maybe you can make 2 process with different exec
Process process1 = Runtime.getRuntime().exec("cmd /c start program1.exe");
Process process2 = Runtime.getRuntime().exec("cmd /c start program2.exe");
if you want this run with same thread please read java books about thread, you can run it at the same time with thread.

How to show command executing in the console and access its output in java

I have a command line tool that queries a server and prints the status of pending jobs to run. This can take up to 30 seconds. I need to call this tool from java and I want to show its output in a text area on my GUI. For the purposes of example, lets say the command is "dir" in windows.
Process.getRuntime().exec("cmd /c dir"); blocks until the command finishes executing, so I thought it would be best to show the command executing in the terminal, then read the output and show it in my text area for future reference. However, the console is hidden giving the user the impression that the application has stopped working. After much research, I have tried:
cmd /k dir - runs in the background and can read output, but application hangs as it requires the /k switch means to keep the window open, but I can't see it to close it.
cmd /c start dir - opens in a new visible terminal, runs but doesn't close. Closing manually doesn't allow me to read the output
cmd /k start dir - same result as above
My question is, how can I spawn a command to run, see it running, and access its output?
Process proc = null;
String[] cmd = { "cmd", "/c", "dir" };
proc = Runtime.getRuntime().exec(cmd);
InputStream inputStream = proc.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null)
{
System.out.println(line);
}
You can grab its input / output / error stream using the process methods. but you need to get the process object - Process p = Process.getRuntime().exec("cmd /c dir"); see this answer also.

Categories