readLine() hangs on last line using Runtime.exec and BufferedStream - java

I am using correct practices mentioned in this article:
http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2
I read the Output and Error streams in concurrently. However, when reading the Output stream of the process, it hangs on readLine() after the last line. I have no idea how to work around this. It is the line of the process and it just hangs there.
The command that hangs this is paexec \192.168.1.92 -c -f C:\Windows\ITBBsync0.bat.
Inside the batch file there are several line such as the following: devcon.exe status =USB > C:\Windows\output.txt
When I execute it on command line, process exits with code 0. When I execute it in Java it hangs after reading the last line of output (which is basically the last line of the batch file). I believe the process is not exiting which is why the issue occurs.
import java.io.*;
class StreamGobbler extends Thread
{
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ((line = br.readLine()) != null){
System.out.println(type + ">" + line);
//hangs after reading last line.
}
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
Also the method that uses the StreamGobbler is the following. There is obviously a parent class which executes this method.
public static boolean cmd(String command){
try{
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + command);
Process p = rt.exec(command);
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(),"ERROR");
StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
errorGobbler.start();
outputGobbler.start();
int exitVal = p.waitFor();
if (exitVal != 0){
System.out.println("ExitValue: " + exitVal);
System.out.println(command + "EXIT CODE:" + exitVal);
return false;
}
return true;
}catch(IOException ioe){
ioe.printStackTrace();
return false;
}catch(InterruptedException ie){
ie.printStackTrace();
return false;
}
}
I have visited many threads on StackOverflow and as far as I know I am using best practices for this. Please let me know how I can fix this or why this may be happening.
I can't give you exactly the application I am executing because it is a bit complex, but the application performs some network operations and also executes a batch file which in turns contains more operations.

Related

In Java, execute other application and receive the screen output

In windows, use Java, mostly we can call Runtime.getRuntime().exec to execute an executable application or batch file, then call proc.getErrorStream() proc.getInputStream() to get the standard output/error stream.
but in this time, I have an application called 'caption2ass.exe' (caption2ass.exe is a well knownd popular tool that can extract ass subtitle from Transport Stream), it prints a lot of information into the screen, but it seems that Java program CAN NOT receive the information by calling proc.getErrorStream() or proc.getInputStream().
Manually I typed 'caption2ass.exe' in the command line, and then I pressed [enter]. after that, the screen will show:
I am trying to receive The infomation in the screen and put it into sysout, or put it into an string array in future.
My Java code is as below:
main program:
String cmd = "E:\\program_media\\Mikey's Fansub Utilities\\TS-OneKeyProcess\\tools\\caption2ass-pcr\\Caption2Ass_PCR.exe";
Runtime run = Runtime.getRuntime();
Process proc = run.exec(cmd);
StreamGobbler errorGobbler = new StreamGobbler(
proc.getErrorStream(), "GBK", "ERR", System.err);
StreamGobbler outputGobbler = new StreamGobbler(
proc.getInputStream(), "GBK", "OUT", System.out);
errorGobbler.start();
outputGobbler.start();
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
StreamGobbler.java :
public class StreamGobbler extends Thread {
InputStream in;
String charsetName;
String type;
PrintStream out;
StreamGobbler(InputStream inputStream, String charsetName, String type, PrintStream out) {
this.in = inputStream;
this.charsetName = charsetName;
this.type = type;
this.out = out;
}
#Override
public void run() {
try {
InputStreamReader isr = new InputStreamReader(in, charsetName);
char[] cbuf = new char[256];
int len = -1;
while ( -1 != (len=isr.read(cbuf))){
out.print(Arrays.copyOf(cbuf, len));
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
IOUtils.closeQuietly(in);
}
}
}
After running this java program, I only got a strange character before ExitValue:
So, My question is: How to get the output information in the screen of this 'caption2ass.exe' using java?
you can get caption2ass from here : http://pan.baidu.com/s/1nuCClXR
, you can run this program in your sandbox if you dare not run an unknown program, especially from Baidu.
Any tests are welcome.
谢谢各位的回复。
I have found the source code of caption2ass, it is a c++ program.
I have changed every logging statment, let them log information into stdout, and then recompiled it.
so i can easily receive the output of caption2ass in the java program, using proc.getInputStream() now.

Java program for retrieving autosys status

Please tell me how to retrieve the status of autosys command from java program.
we can execute the commands using Runtime.getRuntime().exec(command) but how to get autosys status.
I am not sure about the autosys commend but if you can execute using the Runtime then you need to see the output of execution of command. If autosys will return something upon execution you will get it in out InputStream or error in ErrorStream in case of error.
Try something like this:
public static void printStream(InputStream is, String type){
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe){
ioe.printStackTrace();
}
}
public static void main(String args[])
{
String cmd = "command to execute";
Process proc = Runtime.getRuntime().exec(cmd);
printStream(proc.getInputStream(), "OUTPUT");
printStream(proc.getErrorStream(), "ERROR");
}
Process has a method exitValue() to get the exit value for the subprocess. exitValue
Example:
Runtime rt = Runtime.getRuntime();
Process process = rt.exec("ls -al");
process.waitFor();
System.out.println(process.exitValue());

ffmpeg process saying it is erroring

I am using FFMPEG to convert an audio file. I call the command line and in turn FFMPEG from Java. I do this using Runtime and Process Runtime.exec(). For the process I have an InputStream and an ErrorStream. For some reason, even though the FFMPEG commands works fine, it prints out from the Error stream giving the impression it has failed. Anyone know why this might be happening? Obviously it's not a major issue as it does work fine, but if for some reason it does actually error, or someone new to the project does not realise this is how it works it could be confusing.
Any ideas?
Here is the relevant code:
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd);
Process proc = rt.exec(cmd);
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
And the StreamGobbler class:
class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
What you are experiencing is the standard practice across all *NIX tools: they send the "business value" data to stdout and separately send any diagnostic output to stderr. This is an important hallmark of tools written in the "small is beautiful" paradigm, which greatly values the concept of piping one tool's output into the next one's input, thereby creating an ad-hoc chain of information processing. The complete scheme would break down if the tool sent "Processing... 50% done" kind of output to stdout.
Feel free to ignore such output on stderr, but make sure you consume it, as you are doing now, in order to avoid blocking due to overfull output buffers.

Java Runtime.exec() asynchronous output

I'd like to get the output from a long running shell command as it is available instead of waiting for the command to complete. My code is run in a new thread
Process proc = Runtime.getRuntime().exec("/opt/bin/longRunning");
InputStream in = proc.getInputStream();
int c;
while((c = in.read()) != -1) {
MyStaticClass.stringBuilder.append(c);
}
The problem with this is that my program in /opt/bin/longRunning has to complete before the InputStream gets assigned and read. Is there any good way to do this asynchronously? My goal is that an ajax request will return the current value MyStaticClass.stringBuilder.toString()
every second or so.
I'm stuck on Java 5, fyi.
Thanks!
W
Try with Apache Common Exec. It has the ability to asynchronously execute a process and then "pump" the output to a thread. Check the Javadoc for more info
Runtime.getRuntime().exec does not wait for the command to terminate, so you should be getting the output straight away. Maybe the output is being buffered because the command knows it is writing to a pipe rather than a terminal?
Put the reading in a new thread:
new Thread() {
public void run() {
InputStream in = proc.getInputStream();
int c;
while((c = in.read()) != -1) {
MyStaticClass.stringBuilder.append(c);
}
}
}.start();
Did you write the program you're calling? If so try flushing your output after writing. The text could be stuck in a buffer and not getting to your java program.
I use this code to do this and it works:
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while (true) {
// enter a loop where we read what the program has to say and wait for it to finish
// read all the program has to say
while (br.ready()) {
String line = br.readLine();
System.out.println("CMD: " + line);
}
try {
int exitCode = proc.exitValue();
System.out.println("exit code: " + exitCode);
// if we get here then the process finished executing
break;
} catch (IllegalThreadStateException ex) {
// ignore
}
// wait 200ms and try again
Thread.sleep(200);
}
Try :
try {
Process p = Runtime.getRuntime().exec("/opt/bin/longRunning");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line); }

process.waitFor() never returns

Process process = Runtime.getRuntime().exec("tasklist");
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
process.waitFor();
There are many reasons that waitFor() doesn't return.
But it usually boils down to the fact that the executed command doesn't quit.
This, again, can have many reasons.
One common reason is that the process produces some output and you don't read from the appropriate streams. This means that the process is blocked as soon as the buffer is full and waits for your process to continue reading. Your process in turn waits for the other process to finish (which it won't because it waits for your process, ...). This is a classical deadlock situation.
You need to continually read from the processes input stream to ensure that it doesn't block.
There's a nice article that explains all the pitfalls of Runtime.exec() and shows ways around them called "When Runtime.exec() won't" (yes, the article is from 2000, but the content still applies!)
It appears you are not reading the output before waiting for it to finish. This is fine only if the output doesn't fill the buffer. If it does, it will wait until you read the output, catch-22.
Perhaps you have some errors which you are not reading. This would case the application to stop and waitFor to wait forever. A simple way around this is to re-direct the errors to the regular output.
ProcessBuilder pb = new ProcessBuilder("tasklist");
pb.redirectErrorStream(true);
Process process = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null)
System.out.println("tasklist: " + line);
process.waitFor();
Also from Java doc:
java.lang
Class Process
Because 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.
Fail to clear the buffer of input stream (which pipes to the output stream of subprocess)
from Process may lead to a subprocess blocking.
Try this:
Process process = Runtime.getRuntime().exec("tasklist");
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((reader.readLine()) != null) {}
process.waitFor();
I would like to add something to the previous answers but since I don't have the rep to comment, I will just add an answer. This is directed towards android users which are programming in Java.
Per the post from RollingBoy, this code almost worked for me:
Process process = Runtime.getRuntime().exec("tasklist");
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((reader.readLine()) != null) {}
process.waitFor();
In my case, the waitFor() was not releasing because I was executing a statement with no return ("ip adddr flush eth0"). An easy way to fix this is to simply ensure you always return something in your statement. For me, that meant executing the following: "ip adddr flush eth0 && echo done". You can read the buffer all day, but if there is nothing ever returned, your thread will never release its wait.
Hope that helps someone!
There are several possibilities:
You haven't consumed all the output on the process's stdout.
You haven't consumed all the output on the process's stderr.
The process is waiting for input from you and you haven't provided it, or you haven't closed the process's stdin.
The process is spinning in a hard loop.
As others have mentioned you have to consume stderr and stdout.
Compared to the other answers, since Java 1.7 it is even more easy. You do not have to create threads yourself anymore to read stderr and stdout.
Just use the ProcessBuilder and use the methods redirectOutput in combination with either redirectError or redirectErrorStream.
String directory = "/working/dir";
File out = new File(...); // File to write stdout to
File err = new File(...); // File to write stderr to
ProcessBuilder builder = new ProcessBuilder();
builder.directory(new File(directory));
builder.command(command);
builder.redirectOutput(out); // Redirect stdout to file
if(out == err) {
builder.redirectErrorStream(true); // Combine stderr into stdout
} else {
builder.redirectError(err); // Redirect stderr to file
}
Process process = builder.start();
For the same reason you can also use inheritIO() to map Java console with external app console like:
ProcessBuilder pb = new ProcessBuilder(appPath, arguments);
pb.directory(new File(appFile.getParent()));
pb.inheritIO();
Process process = pb.start();
int success = process.waitFor();
You should try consume output and error in the same while
private void runCMD(String CMD) throws IOException, InterruptedException {
System.out.println("Standard output: " + CMD);
Process process = Runtime.getRuntime().exec(CMD);
// Get input streams
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = "";
String newLineCharacter = System.getProperty("line.separator");
boolean isOutReady = false;
boolean isErrorReady = false;
boolean isProcessAlive = false;
boolean isErrorOut = true;
boolean isErrorError = true;
System.out.println("Read command ");
while (process.isAlive()) {
//Read the stdOut
do {
isOutReady = stdInput.ready();
//System.out.println("OUT READY " + isOutReady);
isErrorOut = true;
isErrorError = true;
if (isOutReady) {
line = stdInput.readLine();
isErrorOut = false;
System.out.println("=====================================================================================" + line + newLineCharacter);
}
isErrorReady = stdError.ready();
//System.out.println("ERROR READY " + isErrorReady);
if (isErrorReady) {
line = stdError.readLine();
isErrorError = false;
System.out.println("ERROR::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::" + line + newLineCharacter);
}
isProcessAlive = process.isAlive();
//System.out.println("Process Alive " + isProcessAlive);
if (!isProcessAlive) {
System.out.println(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Process DIE " + line + newLineCharacter);
line = null;
isErrorError = false;
process.waitFor(1000, TimeUnit.MILLISECONDS);
}
} while (line != null);
//Nothing else to read, lets pause for a bit before trying again
System.out.println("PROCESS WAIT FOR");
process.waitFor(100, TimeUnit.MILLISECONDS);
}
System.out.println("Command finished");
}
I think I observed a similar problem: some processes started, seemed to run successfully but never completed. The function waitFor() was waiting forever except if I killed the process in Task Manager.
However, everything worked well in cases the length of the command line was 127 characters or shorter. If long file names are inevitable you may want to use environmental variables, which may allow you keeping the command line string short. You can generate a batch file (using FileWriter) in which you set your environmental variables before calling the program you actually want to run.
The content of such a batch could look like:
set INPUTFILE="C:\Directory 0\Subdirectory 1\AnyFileName"
set OUTPUTFILE="C:\Directory 2\Subdirectory 3\AnotherFileName"
set MYPROG="C:\Directory 4\Subdirectory 5\ExecutableFileName.exe"
%MYPROG% %INPUTFILE% %OUTPUTFILE%
Last step is running this batch file using Runtime.
Here is a method that works for me.
NOTE: There is some code within this method that may not apply to you, so try and ignore it. For example "logStandardOut(...), git-bash, etc".
private String exeShellCommand(String doCommand, String inDir, boolean ignoreErrors) {
logStandardOut("> %s", doCommand);
ProcessBuilder builder = new ProcessBuilder();
StringBuilder stdOut = new StringBuilder();
StringBuilder stdErr = new StringBuilder();
boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows");
if (isWindows) {
String gitBashPathForWindows = "C:\\Program Files\\Git\\bin\\bash";
builder.command(gitBashPathForWindows, "-c", doCommand);
} else {
builder.command("bash", "-c", doCommand);
}
//Do we need to change dirs?
if (inDir != null) {
builder.directory(new File(inDir));
}
//Execute it
Process process = null;
BufferedReader brStdOut;
BufferedReader brStdErr;
try {
//Start the command line process
process = builder.start();
//This hangs on a large file
// https://stackoverflow.com/questions/5483830/process-waitfor-never-returns
//exitCode = process.waitFor();
//This will have both StdIn and StdErr
brStdOut = new BufferedReader(new InputStreamReader(process.getInputStream()));
brStdErr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
//Get the process output
String line = null;
String newLineCharacter = System.getProperty("line.separator");
while (process.isAlive()) {
//Read the stdOut
while ((line = brStdOut.readLine()) != null) {
stdOut.append(line + newLineCharacter);
}
//Read the stdErr
while ((line = brStdErr.readLine()) != null) {
stdErr.append(line + newLineCharacter);
}
//Nothing else to read, lets pause for a bit before trying again
process.waitFor(100, TimeUnit.MILLISECONDS);
}
//Read anything left, after the process exited
while ((line = brStdOut.readLine()) != null) {
stdOut.append(line + newLineCharacter);
}
//Read anything left, after the process exited
while ((line = brStdErr.readLine()) != null) {
stdErr.append(line + newLineCharacter);
}
//cleanup
if (brStdOut != null) {
brStdOut.close();
}
if (brStdErr != null) {
brStdOut.close();
}
//Log non-zero exit values
if (!ignoreErrors && process.exitValue() != 0) {
String exMsg = String.format("%s%nprocess.exitValue=%s", stdErr, process.exitValue());
throw new ExecuteCommandException(exMsg);
}
} catch (ExecuteCommandException e) {
throw e;
} catch (Exception e) {
throw new ExecuteCommandException(stdErr.toString(), e);
} finally {
//Log the results
logStandardOut(stdOut.toString());
logStandardError(stdErr.toString());
}
return stdOut.toString();
}
Asynchronous reading of stream combined with avoiding Wait with a timeout will solve the problem.
You can find a page explaining this here http://simplebasics.net/.net/process-waitforexit-with-a-timeout-will-not-be-able-to-collect-the-output-message/
public static void main(String[] args) throws PyException, IOException, InterruptedException
these should be the exceptions thrown

Categories