Java - How to read output from 7z? - java

Hy.
I've created a routine that read .tgz files from a directory and unzip each one. I'm using
Process zip01 = Runtime.getRuntime().exec("LINE OF COMMAND");
and
exitVal = zip01.waitFor();
I,m using 7z.exe from its folder to decompress and compress files. The command line is working fine. Now, I what to read the percentage of the decompress and throw it into a textfield or a textarea. The graphics part are ok too, s well all the routine. The only dificult is to get the realtime percentage of the 7z. is there some way to read and show it?
Thanks!

You can get the output of your process like this:
Process zip01 = Runtime.getRuntime().exec("LINE OF COMMAND");
BufferedReader output = new BufferedReader(new InputStreamReader(zip01.getInputStream()));
String line;
while ((line = output.readLine()) != null) {
/* process lines */
}

Related

How can I get BufferedReader to display live outputs from a running python script using ProcessBuilder?

I have a Java script that starts a new thread to execute a python script using Process builder. The code below currently takes the output from python and displays it in the Java run output and within a JTextArea. BUT, it only does so in bulk, once the py script has finished running. Is there a way to get the output displayed live as it is written out from the py script? Thanks!!!!
public void launchPythonScript() {
try {
ProcessBuilder py = new ProcessBuilder("cmd", "/C", "PythonScriptLocation (C:\\....)",""+Directory(variable needed for py script));
Process launch = py.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(launch.getInputStream()));
String readLine;
StringBuilder JavaOutput = new StringBuilder();
while((readLine = reader.readLine()) != null){
JavaOutput.append(readLine).append(System.lineSeparator());
frame2.consoleOutput.setText(JavaOutput.toString());
System.out.println(readLine);
}
} catch (IOException ex) { Logger.getLogger(Frame1.class.getName()).log(Level.SEVERE, null, ex);}
}
Process InputStream should give you data while the script is running.
You are reading one line at time so if your python script send all data in one single line then you see the output only at the end.
Try to break the output data into multiple lines.

Processes of ProcessBuilder are not writing some output files (Java)

I am running some .jar programs from a Java code. To do that, I use ProcessBuilder, as follows:
ProcessBuilder pb = new ProcessBuilder("java", "-jar", jarFile, configFile);
Process p = pb.start();
finished = p.waitFor(600, TimeUnit.SECONDS);
// print the error output
if(finished){
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
result = builder.toString();
reader.close();
status = p.exitValue();
}
else{
p.destroyForcibly();
result = "Error: maximum time (600 seconds) exceeded.";
}
The above code is within a foor bucle that selects different jar files (using the jarFile variable) at each iteration. Each one of the jar files writes some output files (note that these output files are different from the standard output / standard error of the program, they are other additional output files).
The problem is that, sometimes, some of the jar programs do not create its corresponding files. If I re-run the code several times, the processes that write their output files are different in each execution (which is very rare).
Could someone give me an indication on how to solve this problem? Thanks in advance.

Redirect python output log to Java GUI dynamically

I have designed a GUI in Java using Swing.
Using GUI I read the location as inputs
Using these inputs as parameter I call a Python Script from this Java Code
Now, I need to display the output of the python script on GUI dynamically.
As the python script runs the output log of the script has to be displayed on the GUI area simultaneously.
Is there anyway I can do that ?
Please help
A code would be useful, however, you could write the python script output on a file and than read that output from that file to the Java GUI.
Python
out_file = open("test.txt","w")
out_file.write("This Text is going to out file\nLook at it and see\n")
out_file.close()
JAVA
File name = new File("C:/path/test.txt");
if (name.isFile()) {
try {
BufferedReader input = new BufferedReader(new FileReader(name));
StringBuffer buffer = new StringBuffer();
String text;
while ((text = input.readLine()) != null){
buffer.append(text + "\n");}
input.close();
System.out.println(buffer.toString());
} catch (IOException ioException) {}
}

Sending input to stdin and getting the full output in Java - Festival TTS

I'm trying to use the Java Runtime.getRuntime().exec(String) command to run Festival, then use OutputStreamWriter to write some commands to the outpustream of the process.
This works great, and I'm able to do something like this:
Process p = Runtime.getRuntime().exec("festival");
Writer w = new OutputStreamWriter(p.getOutputStream());
w.append("(SayText \"Hello World\")");
w.flush();
Obviously the way I can tell this works is that it speaks the text through the speakers.
What I am having a real hard time doing is getting the text output from what I would see in the terminal. I'm trying to run some other commands (such as (voice.list)) which output text, presumably to stdout.
For example, I've tried using a BufferedReader in the following way:
BufferedReader reader = new BufferedReader (new InputStreamReader(p.getInputStream()));
w.append("(voice.list)");
w.flush();
String output = "";
String line = reader.readLine();
System.out.println(line);
while ((line = reader.readLine()) != null)
{
System.out.println("Reading: " + line);
output += line;
}
(The System.out.println's is just for debugging, I would do the entire thing in a cleaner way if I was able to get it to work.)
No matter what code I try, I'm never able to get any output from Festival. I can get output from other commands. E.G. I have tried this code as well http://en.allexperts.com/q/Java-1046/2008/2/Runtime-getRuntime-exec-cmd.htm and it works with many other commands (like ls) but not Festival.
Does anything have any idea how I would be able to get this to work?
Thanks.
Festival may output it's text on stderr instead of stdout. Try replacing
p.getInputStream()
with
p.getErrorStream()

Communication between unix commands in Java

In one commend , I'm trying to send data to System.out like this:
And in another command I'm trying to get this data from System.in.
It's strange because, it works once of many tries. I can try to run it 10 times and it's still inReader.ready() == false, and when I run it for example 11th time , it works.
Why ? How can I fix this? How to make it work everytime ?
Thanks, in advance !
You can't read your InputStream that way, since the data may not have been arrived at the second process yet. You can either read character by character, with something like:
InputStreamReader inReader = new InputStreamReader(System.in);
int data = inReader.read();
while (data != -1){
...
data = inReader.read();
}
or simple read the input line by line, using:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while ((String line = br.readLine()) != null) {
...
}
If your objective is to execute a shell command, don't use System.out but Runtime.getRuntime().exec(cmd) instead. Check out this question for more details.

Categories