This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes runtime exec - mplayer doesn't complete Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "runtime exec - mplayer doesn Watch "runtime exec - mplayer doesn New topic

runtime exec - mplayer doesn't complete

Matt Zollinhofer
Ranch Hand

Joined: Jul 09, 2004
Posts: 33
I'm having problems with the exec command. I have mplayer set up to download a stream to disk (thus allowing me to listen to a live broadcast on my ipod later). I was trying to wrap this functionality into a small java program to do a few other things with it. The only way I know to run a unix program, mplayer, from java is with Runtime.exec().

So here is what I have:
Runtime rt;
rt = Runtime.getRuntime();
Process p = rt.exec("mplayer -playlist ~/Desktop/new.txt -ao pcm -aofile ~/mystream.wav -vc dummy -vo null");
Process p = rt.exec(arg);
int val = p.waitFor();
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
while ( (line = br.readLine()) != null )
System.out.println("2:rtn Val: "+val);
catch (Exception e)

and the output is:
matt:~/development/java/StreamToMP3 matt$ java StreamToMP3
MPlayer dev-CVS-040614-08:32-3.3 (C) 2000-2004 MPlayer Team

AltiVec found
CPU: PowerPC
Reading config file /usr/local/etc/mplayer/mplayer.confReading config file /Users/matt/.mplayer/config
2: rtn Val: 1

As you can see, it starts to run the mplayer command. But, it doesn't finish. The file it tries to download is not complete, in fact it never even creates the file "mystream.wav". I would think that using the p.waitFor() would make this happen, but it doesn't seem to.

Any thoughts?

[ July 09, 2004: Message edited by: Matt Zollinhofer ]
[ July 09, 2004: Message edited by: Matt Zollinhofer ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Just guessing: You may have trouble that waitfor() waits for the process to finish, but it can't finish because you aren't reading from the outputs yet. Put your reader for stdout on another thread and fire it up before you waitfor(). And make another one for errout just in case. You might have good luck with join() on the reader threads instead of waitfor(). I had one program where the readers finished well after waitfor() returned.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Matt Zollinhofer
Ranch Hand

Joined: Jul 09, 2004
Posts: 33
So, I put the output stuff in a different thread, but it didn't seem to help. It looks like mplayer is returning early, but I don't get any feedback while. If you notice above, I print out what the process returns, and it returns a 1, not the standard 0 for exiting properly.

I don't know why mplayer would return something if it wasn't finished, but that seems to be whats happening. Am I totally missing something?

Matt Zollinhofer
Ranch Hand

Joined: Jul 09, 2004
Posts: 33
Also, it doesn't hang at all. It just goes right through and terminates as if everything was fine. But it didn't do everything that the mplayer should have done if it was finished. And of course I did test the mplayer command in the command line by itself to make sure that it was a valid command.
I agree. Here's the link:
subject: runtime exec - mplayer doesn't complete
It's not a secret anymore!