Reading InputStream from Java Process - java

Im trying to open a terminal console, and be able to read / write commands to it.
I read some questions like:
Java Process with Input/Output Stream
With that was able build a little app that opens the terminal and pass commands to the console and print the result back, it works well with any system comand like browsing folders, deleting files and stuff like that.
The problem I have is that I need to load another java program from that console and read its output but that program uses java.util.logging.Logger to send most of its output and for some reason my launching app can't read what Logger prints.
Basically Im trying to build like a wrapper for another java app, because I want to interact with it but cant modify it.
Thanks for your help.
EDIT
Here is the code, but its basically taken from another questions, also as I said it works for things in the "normal" stdout, but not for the output Logger prints to the console.
package launcher;
import java.io.*;
import java.util.Scanner;
public class Launcher {
public static void main(String[] args) throws IOException {
String line;
Scanner scan = new Scanner(System.in);
Process process = Runtime.getRuntime().exec("/bin/bash");
OutputStream stdin = process.getOutputStream();
InputStream stderr = process.getErrorStream();
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
while (scan.hasNext()) {
String input = scan.nextLine();
if (input.trim().equals("exit")) {
writer.write("exit\n");
} else {
writer.write("((" + input + ") && echo --EOF--) || echo --EOF--\n");
}
writer.flush();
line = reader.readLine();
while (line != null && !line.trim().equals("--EOF--")) {
System.out.println("Stdout: " + line);
line = reader.readLine();
}
if (line == null) {
break;
}
}
}
}

Without seeing any code/config, I would guess that either the logger is configured to write to stderr (System.err) and you're only reading stdout (System.out), or else the logger is configured to write to a file.

Per dty's answer, I think by default java.util.logging uses stderr, so you should redirect stderr to stdout like this:
ProcessBuilder builder = new ProcessBuilder("ls", "-l"); // or whatever your command is
builder.redirectErrorStream(true);
Process proc = builder.start();
FWIW in my experience, you'd be better off trying to use the other Java program by starting its main method in your own program, than trying to wrestle with input/output streams etc., but that depends on what the other program does.

Related

Java: is there a way to run a system command and print the output during execution?

I have a python script and it takes a long time to finish. I would like to run it from Java, but also output the script's output while it is executing, so that I can tell if it is properly running.
I've searched and only found examples where we output the output after the system command has finished, rather than during its execution.
Any way to do it while the script is running?
Here's what I have
public void doSomething() throws IOException {
String[] callAndArgs = {"python", "/hi.py"};
Process p = Runtime.getRuntime().exec(callAndArgs);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String s;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
i managed to get it working like this (Note it requires java7):
package test;
import java.lang.ProcessBuilder.Redirect;
public class Test {
public static void main(String... args) throws Exception {
ProcessBuilder pb = new ProcessBuilder("python","/home/foobar/Programming/test/src/test/test.py");
pb.redirectOutput(Redirect.INHERIT);
Process p = pb.start();
p.waitFor();
}
}
python (note i flush on python to make it work using sys.stdout.flush())
import time,sys
c =0
while c<=50:
time.sleep(1)
print("----")
c = c +1
sys.stdout.flush()
Note if you don't want to flush in a loop you can use this:
ProcessBuilder pb = new ProcessBuilder("python","-u","/home/foobar/Programming/NetBeansProjects/test/src/test/test.py");
Redirect.INHERIT
Indicates that subprocess I/O source or destination will be the same as those of the current process. This is the normal behavior of most operating system command interpreters (shells).
I've searched and only found examples where we output the output after
the system command has finished, rather than during its execution.
That's weird, because your example should be dumping the output as the command is executing.
Instead of using BufferedReader, you could try reading directly from the InputStream instead as the required conditions for readLine might not be being met until after the process exits.
I'd also recommend that you use a ProcessBuilder over Process directly, as, apart from anything else, it allows you to redirect the output from the error stream into the input stream, allowing you to read just one stream instead of two...
This might also be an issue with Python and how it flushes it output buffers...
For example, rather then waiting for the BufferedReader to decide when to return, try printing each character from the stream as it occurs/is reported
ProcessBuilder pb = new ProcessBuilder("test.py");
pb.redirectError();
Process p = pb.start();
InputStream is = null;
try {
is = p.getInputStream();
int in = -1;
while ((in = is.read()) != -1) {
System.out.print((char)in);
}
} finally {
try {
is.close();
} catch (Exception e) {
}
}
Update
Doing a little reading, Python seems to be buffering its out out before sending it to the stdout. I don't think you can fix this on the a Java side, but need to alter either the way Python is run or the script works.
See How to flush output of Python print? for more details
I'm suspecting that you are writing to stderr, which you can't see because you are blocking on stdin. Use a ProcessBuilder instead of doing exec. This way, you can redirect stderr and stdin into a single stream.
Here is an example:
import java.io.*;
public class Test {
public static void main(String... args) throws IOException {
ProcessBuilder pb =
new ProcessBuilder("test.py");
pb.redirectErrorStream(true);
Process proc = pb.start();
Reader reader = new InputStreamReader(proc.getInputStream());
BufferedReader bf = new BufferedReader(reader);
String s;
while ((s = bf.readLine()) != null) {
System.out.println(s);
}
}
}
Alternatively you can spawn threads to read from stdin/stderr respectively.
Another thing to look for is output buffering by python. You can see if this is the cause by doing:
import sys
sys.stdout.flush()
after you write to stdout
Don't use #readLine as the conditional in your while loop. Instead wrap your inputStream in a scanner and use #hasNextLine()
Scanner in = new Scanner(p.getInputStream());
while (in.hasNextLine()) {
System.out.println(in.nextLine());
}

How to print logs from another jar line by line?

I have created a Java application that has 2 Jar files. Jar1 is used to initialize and run Jar2, using this code :
Process process = runtime.exec( "java -jar Jar2.jar" );
printLogs( process );
.
.
.
private static boolean printLogs( Process process ) {
try {
BufferedInputStream logStream = new BufferedInputStream( process.getInputStream() );
String logs = "";
int buffer = 0;
while ( ( buffer = logStream.read() ) != -1 ) {
logs += (char)buffer;
}
if( !logs.isEmpty() ) logger.debug( logs );
} catch (IOException e) {}
return true;
}
I print many logs from Jar2 using Log4J, i.e.
logger.debug( "..." );
But none of the logs in Jar2 were printed to the console. I figured out it's because the logs are returned to Jar1 and not to the console, So i printed the returning stream using the above code. Logs are now printed fine but after all Jar2 process ends up, then all logs are printed at once in Jar1.
The question is: Can i print each log line in Jar2 in time instead of waiting all Jar2 process to end ?
Because Jar2 is a long process, and it is important that i can see those logs while the application is processing.
The whole thing is quite messed up. You shouldn't need two separate archives and Runtime.exec()
However, one usually uses BufferedReader.readLines to read text lines. Note that the problem simply vanishes if you log each line at the moment you read it:
BufferedReader input = new BufferedReader(
new InputStreamReader(process.getInputStream())
);
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
Your code waits to the child process to complete because you logged the line after the stream ends (ie after the subprocess has terminated)
Here is a demo program which uses a long running Ruby program as the watched process
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Subprocess {
static final String[] program = new String[] {
"ruby",
"-e" ,
"(1..5).each{|i|sleep 1;puts i;STDOUT.flush}"
};
public static void main(String[] args) throws IOException {
ProcessBuilder builder = new ProcessBuilder(program);
builder.redirectErrorStream();
Process child = builder.start();
String line = null;
BufferedReader in = new BufferedReader(
new InputStreamReader(child.getInputStream()));
while ((line = in.readLine()) != null)
System.out.println(line);
}
}
Your code seems to be waiting for the logStream to get to EOF before actually writing logs (which occurs when the process exits). Try refactoring it to read it character-by-character, then logging the accumulated character buffer whenever you see a newline (and EOF, of course - so you get the last line).
With the help of this post i was able to fix this :
Runtime.exec never returns when reading system.in
I have used the ProcessBuilder too.

Reading InputStream from Java Interactive Process using ProcessBuilder

I am trying to run an interactive executable from Java application using ProcessBuilder; it's supposed to take input, produce output and then wait for the next input. The main problem here with Input/Output streams. I send an input and get nothing. Here is the code:
private static Process process;
private static BufferedReader result;
private static PrintWriter input;
process = new ProcessBuilder("compile-lm", lmFile.toString(), " --score yes").redirectErrorStream(true).start();
input = new PrintWriter(new OutputStreamWriter(process.getOutputStream()), true);
input.println(message);
System.out.println(message);
result = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = new String();
while ((line = result.readLine()) != null)
{
/* Some processing for the read line */
System.out.println("output:\t" + line);
}
I have tried your code it works fine there is no problem with the code. I think that the problem with the command that you are trying to execute ( it returns nothing ). try to change parameters or even change the entire command to test. and if you can execute the comand in other place ( terminal for example try it and see the output with the same parameters )
I have used a similar setup many times over but can not find a working copy right now :( My first instinct though is to move the line where you initialise the reader (result variable) to before the one where you send the command out to the process (input.println(message)).
Try closing the output stream to the process. Basically you're at the mercy of whatever buffering is happening in the output side of the child process.

Why the command line utility hangs when invoked through java program?

I need your suggestions and guidance in following task.
I am using libdmtx which comes with a command line utility which reads the image files for ECC200 Data Matrix barcodes, reads their contents, and writes the decoded messages to standard output.
I want to use this command line utility in my java program on linux platform. I amd using ubuntu linux. I have installed the libdmtx on my linux machine. and when I invoke the command
dmtxread -n /home/admin/ab.tif
on linux terminal it gives the decoded value of barcode in image immediately.
when I am going to invoke this command using my java program the code stuks in execution of the command and dotn gives output.
it looks like the program is processing or got hang.
Following is my java code which invokes the following command
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class Classtest {
public static void getCodes(){
try
{
Process p;
String command[]=new String[3];
command[0]="dmtxread";
command[1]="-n";
command[2]="/home/admin/ab.tif";
System.out.println("Command : "+command[0]+command[1]+command[2]);
p=Runtime.getRuntime().exec(command); //I think hangs over here.
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line=reader.readLine();
if(line==null){
reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
line=reader.readLine();
System.out.print("Decoded :- "+line);
}else{
System.out.print("Error :- "+line);
}
System.out.println(p.waitFor());
}catch(IOException e1) {
e1.getMessage();
e1.printStackTrace();
}catch(InterruptedException e2) {
e2.getMessage();
e2.printStackTrace();
}
}
public static void main(String args[]){
getCodes();
}
}
Please tell me friends where my code is going wrong.
I refered to following article but dint get any help
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1
Please guide me friends!
Thank you!
Here is the new code in which I used the ProcessBuilder Class this code also giving the same output as above code that is it hangs at the line
Process process = pb.start();
public class Test {
public static void main(final String[] args) throws IOException, InterruptedException {
//Build command
List<String> commands = new ArrayList<String>();
commands.add("dmtxread");
commands.add("-n");
commands.add("/home/admin/ab.tif");
System.out.println(commands);
//Run macro on target
ProcessBuilder pb = new ProcessBuilder(commands);
pb.redirectErrorStream(true);
Process process = pb.start();
//Read output
StringBuilder out = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null, previous = null;
while ((line = br.readLine()) != null){
System.out.println(line);
}
//Check result
if (process.waitFor() == 0)
System.out.println("Success!");
System.exit(0);
//Abnormal termination: Log command parameters and output and throw ExecutionException
System.err.println(commands);
System.err.println(out.toString());
System.exit(1);
}
}
Please guide me to solve this problem.
Thanks You!
The readLine blocks until it receives a new line from the error stream. So, if there is no output, your program won't get past the first readLine.
For simplicity I would recommend you use a ProcessBuilder instead of Runtime.exec(), which lets you merge the two InputStreams as follows:
ProcessBuilder builder = new ProcessBuilder(cmd,arg0,arg1);
builder.redirectErrorStream(true);
Process process = builder.start();
So, now you can just read from one.
Alternatively you can use separate threads to consume the two InputStreams.
Hope that helps
Your stream-consumption code is very confused. You try to read a single line from the stderr, then abandon that reader, then try to read a single line from the stdout.
If the program doesn't print anything to stderr, you'll hang at line 2.
If the program sends too much stuff to stderr so it fills its buffer, then the program itself will block and your Java will block at waitFor.
Both of these apply to stdout.
The proper way to consume the process's output streams is covered in detail in that article you have linked. Take that advice, nobody can give you better advice than that.
I am not sure what exactly happens with your program and where does it hang (you could use a debugger or trace output to check that), but here is the possible scenario:
Imagine that the program wants to output 2 lines of text. Or only one line but into stderr. Your code reads only 1 line fro stdout and than waits for the process to exit. This means that the child program may wait for the reader to read the next line, so it waits in write until someone unblocks the pipe -- forever.
When you run dmtxread from command line, there is no blocking on output pipe, so the program runs just finely.

Start external executable from Java code with streams redirection

I need to start external executable in such way that user can interact with program that was just started.
For example in OpenSuse Linux there is a package manager - Zypper. You can start zypper in command mode and give commands like install, update, remove, etc. to it.
I would like to run it from Java code in a way user could interact with it: input commands and see output and errors of the program he started.
Here is a Java code I tried to use:
public static void main(String[] args) throws IOException, InterruptedException {
Process proc = java.lang.Runtime.getRuntime().exec("zypper shell");
InputStream stderr = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
char ch;
while ( (ch = (char)br.read()) != -1)
System.out.print(ch);
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
}
But unfortunately I can only see it's output:
zypper>
but no matter what I write, my input doesn't affect program that was started.
How can I do what want to?
You need to get an output stream in order to write to the process:
OutputStream out = proc.getOuptutStream();
This output stream is piped into the standard input stream of the process, so you can just write to it (perhaps you want to wrap it in a PrintWriter first) and the data will be sent to the process' stdin.
Note that it might also be convenient to get the error stream (proc.getErrorStream) in order to read any error output that the process writes to its stderr.
API reference:
http://download.oracle.com/javase/6/docs/api/java/lang/Process.html
Seems like the converting inside the while condition fails in your example, this seems to work better (I don't run Suse so I haven't tried with Zypper):
public static void main(String[] args) throws IOException, InterruptedException
{
//Process proc = java.lang.Runtime.getRuntime().exec("zypper shell");
Process proc = java.lang.Runtime.getRuntime().exec("ping -t localhost");
InputStream stderr = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
int i;
while ( (i = br.read()) != -1)
{
System.out.print((char) i);
}
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
}
I recently wrapped Google Closure Compiler into a .jar-file which is extracted and used in a Process. This compiler only talks via System.in/out/err. There's a big "gotcha" in connecting pipes together, which is just briefly mentioned in the Process javadoc.
"...failure to promptly write the
input stream or read the output stream
of the subprocess may cause the
subprocess to block, and even
deadlock."
On Mac OS X the buffer is 16k, and if you don't read it promptly as suggested, the process deadlocks. My only solution to this problem ATM, is a rather nasty busy wait.
https://github.com/algesten/googccwrap/blob/master/src/main/java/googccwrap/GoogleClosureCompilerWrapper.java

Categories

Resources