Runtime.exec() is not working right

I found a command line program that allows me to encode mov to mpeg. The problem now is, whenever I try to execute it using exec, the mpeg file does appear but it is not viewable until I close my IDE. Anybody knows why?Thanks.
[240 byte] By [zactoraa] at [2007-10-3 4:55:40]
# 1
[url] http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html[/url]
bckrispia at 2007-7-14 23:00:51 > top of Java-index,Java Essentials,Java Programming...
# 2
I read that article a few times but I still can't identify the problem. :(
zactoraa at 2007-7-14 23:00:51 > top of Java-index,Java Essentials,Java Programming...
# 3
Have you confirmed that the process has actually completed? If not, confirm that first. If it's not completing make sure it's not deadlocked due to the output or error streams.
kablaira at 2007-7-14 23:00:51 > top of Java-index,Java Essentials,Java Programming...
# 4

public void run()

{

Runtime rt = Runtime.getRuntime();

try {

Process cmd = rt.exec("cmd /c C:\\mplayer\\mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=xvcd -vf scale=512:512,harddup -srate 44100 -af lavcresample=44100 -lavcopts vcodec=mpeg1video:keyint=15:vrc_buf_size=327:vrc_minrate=1152:vbitrate=1152:vrc_maxrate=1152:acodec=mp2:abitrate=224 -ofps 25 -o \"C:\\Documents and Settings\\Alvin Loh\\My Documents\\MP-SIP\\Server (Secure With JpegImagesToMovie)\\videos\\Nov2005\\test\\abc.mpg\" \"C:\\Documents and Settings\\Alvin Loh\\My Documents\\MP-SIP\\Server (Secure With JpegImagesToMovie)\\videos\\Nov2005\\test\\abc.mov\"");

} catch (IOException ex) {

ex.printStackTrace();

}

}

I'm currently running the exec method as a seperate thread. How do I check if the process is completed? Sorry, I'm still new to Java.

zactoraa at 2007-7-14 23:00:51 > top of Java-index,Java Essentials,Java Programming...
# 5

public void run() {

Runtime rt = Runtime.getRuntime();

try {

Process cmd = rt.exec("...snip...");

System.out.println("Command finished with value: " + cmd.waitFor());

} catch (IOException ex) {

ex.printStackTrace();

} catch (InterruptedException ex) {

System.out.println("INTERRUPTED while waiting on command!");

}

}

kablaira at 2007-7-14 23:00:51 > top of Java-index,Java Essentials,Java Programming...
# 6
I just tried that but the value did not show in the console.
zactoraa at 2007-7-14 23:00:51 > top of Java-index,Java Essentials,Java Programming...
# 7

Then the process is not completing. Read the article posted earlier again and follow the advice given in regards to the error and output streams. There's a chance your process is blocking because the pipe is full. Since it's not exiting there may be resources it's still holding on to resources which prevent you from viewing the movie. Killing your IDE probably results in killing the blocked process too, since it was launched from there.

kablaira at 2007-7-14 23:00:51 > top of Java-index,Java Essentials,Java Programming...
# 8

public void run()

{

try

{

Runtime rt = Runtime.getRuntime();

Process proc = rt.exec(...cmd...);

InputStream stderr = proc.getErrorStream();

InputStreamReader isr = new InputStreamReader(stderr);

BufferedReader br = new BufferedReader(isr);

String line = null;

System.out.println("<ERROR>");

while ( (line = br.readLine()) != null)

System.out.println(line);

System.out.println("</ERROR>");

int exitVal = proc.waitFor();

System.out.println("Process exitValue: " + exitVal);

}

catch (Throwable t)

{

t.printStackTrace();

}

}

It's not working still. :(

Any idea why?

zactoraa at 2007-7-14 23:00:51 > top of Java-index,Java Essentials,Java Programming...
# 9

That takes care of the error stream, what about the output stream? Keep reading the article, that's the mediocre solution for a reason. Use StreamGobbler which is described later in the article.

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();

}

}

}

public void run() {

Runtime rt = Runtime.getRuntime();

try {

Process cmd = rt.exec("...snip...");

new StreamGobbler(cmd.getInputStream(), "OUTPUT").start();

new StreamGobbler(cmd.getErrorStream(), "ERROR").start();

System.out.println("Command finished with value: " + cmd.waitFor());

} catch (IOException ex) {

ex.printStackTrace();

} catch (InterruptedException ex) {

System.out.println("INTERRUPTED while waiting on command!");

}

}

kablaira at 2007-7-14 23:00:51 > top of Java-index,Java Essentials,Java Programming...
# 10
Hey thanks! It's working fine now!Cheers! :)
zactoraa at 2007-7-14 23:00:51 > top of Java-index,Java Essentials,Java Programming...
# 11
Just one more question if you don't mind. How do I know if the process is completed? There are still other things that needs to be implemented after the encoding.
zactoraa at 2007-7-14 23:00:51 > top of Java-index,Java Essentials,Java Programming...
# 12
you can use the waitFor method to wait until the process is over.Also when you read the inout stream and the error stream of the process you will hit EOF once the process is over.
LRMKa at 2007-7-14 23:00:51 > top of Java-index,Java Essentials,Java Programming...