process.waitFor is stuck - java

In below java code, I see that process.waitFor() seems to not exit. I have set processBuilder.redirectErrorStream(true); which should help me to redirect error with standard output and am reading the inputStream in method readProcessOutput. I thought when we read all the output and error streams the process should exit. But currently its struck. Can I please know how to solve this?
Also, I am using which sets standard I/O to be the same as those of the current Java process.
...
ProcessBuilder processBuilder = new ProcessBuilder().inheritIO().command("sh",
"test.sh");
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
List<String> results = readProcessOutput(process.getInputStream());
setExitCode(process.waitFor());
...
private List<String> readProcessOutput(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 1);
List<String> lines = new ArrayList<>();
String line;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
inputStream.close();
bufferedReader.close();
return lines;
}
public int getExitCode() {
return exitCode;
}
private void setExitCode(int exitCode) {
this.exitCode = exitCode;
}

Related

Java executing C application with arguments and get back the return value?

Hi i am trying to build a java program but i need run a C program with arguments and after i need get the return value..
Here is what i have done.
public static void main(String[] args) throws IOException {
ProcessBuilder pr = new ProcessBuilder("C:\\Users\\MyFolder\\Desktop\\MyExe.exe", "param1", "param2");
Process process = pr.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
You can use:
int retVal = process.waitFor()
to block execution until the process is finished, and returns the return value.
See the docs for Process here: http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#waitFor()

How do I redirect from console (including error and output) to a file in Java?

package online_test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class cmdline_test {
/**
* #param args
*/
public static void main(String[] args) {
try {
String[] command = new String[3];
command[0] = "cmd";
command[1] = "/c";
command[2] = "c: && dir && cd snap";
Process p = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String Error;
while ((Error = stdError.readLine()) != null) {
System.out.println(Error);
}
while ((Error = stdInput.readLine()) != null) {
System.out.println(Error);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
When I run this code, I get the output of this code printed to the console. However, I wasn't able to figure out how to copy that output to a file. How would I go about doing so?
Use a ProcessBuilder:
final File outputFile = Paths.get("somefile.txt").toFile();
final ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "whatever")
.redirectOutput(outputFile)
.redirectErrorStream(true);
final Process p = pb.start();
// etc
Read the javadoc carefully; there is a lot more you can do with it (affecting the environment, changing the working directory etc).
Also, do you really need to go through an interpreter at all?
A more simple solution is to change the outputstream of System.out to a file. This way, every time you invoke System.out.println(...) it will write to said file. Add this to the start of your program:
File file =
new File("somefile.log");
PrintStream printStream = null;
try {
printStream = new PrintStream(new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.setOut(printStream);
You can do the same for System.err for printing errors to a different file.

Read/Write to command-line .exe Java

I'm trying to launch a process in java, read the output, write to the program, then read what it responds with. From all the other answers on SO, this is what I have come up with:
class Main
{
public static void main(String[] args) {
String line = "";
try {
ProcessBuilder pb = new ProcessBuilder("C:\\myProgram.exe");
Process p = pb.start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
output.write("foo");
output.newLine();
output.flush();
while ((line = input.readLine()) != null) {
System.out.println(line);
}
p.destroy();
}
catch (IOException e){
}
}
}
It launches the program, and gives me the output just as expected.
When i write foo, I expect the program to come back with another response, but it never does.
What am I doing wrong?

Java Process can't get ErrorStream message

everyone, I have a process that needs to get standard output and log/error/exception output from the subprocess. The standard output is fine, but I can't get ErrorStream, therefore the program is stuck there because of that. Here is my simple code. There is nothing magic, but why can't I get the error stream here? Thanks for looking at it.
BufferedReader standard =
new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader error =
new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
while ((line = standard.readLine()) != null) {
System.out.println(line);
}
while ((line = error.readLine()) != null) {
System.out.println(line);
}
Now, as suggested, i used two threads to process the output and error streams, but still had the same problem, as follows. Can anybody give me some insights? Thanks.
ProcessBuilder pb = new ProcessBuilder(listArgs);
pb.redirectErrorStream();
Process process = pb.start();
StreamThread output = new StreamThread(process.getInputStream());
StreamThread error = new StreamThread(process.getErrorStream());
output.start();
error.start();
while (true) {
try {
output.join();
break;
}
catch (InterruptedException ie) {
ie.printStackTrace();
}
}
The definition of the StreamThread:
public static class StreamThread extends Thread{
private InputStream input = null;
public StreamThread(InputStream in){
input = in;
}
String line = null;
public void start(){
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
try{
while( (line=reader.readLine()) != null ){
System.out.println(line);
}
reader.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
Look at your loops:
while ((line = standard.readLine()) != null) {
System.out.println(line);
}
while ((line = error.readLine()) != null) {
System.out.println(line);
}
You're going to keep reading from the output stream until it's finished - which is likely to be when the process terminates. Only then do you start reading the error stream.
You should probably put at least one of these into a different thread, so you can read from both streams at the same time.

Print Runtime.getRuntime().exec() output live

I'm writing a Java program that is used to call a PHP script in set intervals. The PHP script outputs a lot of data, and it is the client's requirement that the Java program displays all the PHP script's output while the script runs.
The method I'm using is:
Runtime.getRuntime().exec(new String[]{"php", "file.php"});
Then using an InputStreamReader to grab output, primarily using examples from here. The problem I'm having is that the stream reader only outputs the data once the PHP script exits (which makes sense considering how the output is looped through).
How would I go about printing the script's output live while the script is running?
I did this by reading the output from a separate thread:
Process p = Runtime.getRuntime().exec(commands);
final InputStream stream = p.getInputStream();
new Thread(new Runnable() {
public void run() {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(stream));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
// TODO
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// ignore
}
}
}
}
}).start();
For now I decided to go with Andrew Thompson's suggestion:
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
Process process = builder.start();
InputStreamReader istream = new InputStreamReader(process.getInputStream());
BufferedReader br = new BufferedReader(istream);
String line;
while ((line = br.readLine()) != null){
System.out.println(line);
}
process.waitFor();
This is still not what I'm looking for though, so for now I'm leaving the question unanswered.

Categories