Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Runtime.exec()  RSS feed

 
Jeff Holmes
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is a method that is invocted when a button receives an actionevent.
public void compileFiles() {
String cmds[] = new String[1];
cmds[0] = "C:\\jdk1.3\\bin\\javac.exe";
Runtime rt = null;
Process pro = null;
try {
System.err.println("1");
rt = Runtime.getRuntime();
System.err.println("1.5");
pro = rt.exec(cmds);
System.err.println("2");
pro.waitFor();
System.err.println("Done");
InputStream out = pro.getInputStream();
int i;
while ((i = out.read()) != -1) {
System.err.print((char)i);
}
} catch (Exception e) {
System.err.println("Caugth Exception: " + e.getMessage());
}
}
The problem I am having is that it makes it past the System.err.println("2") but never goes beyond the pro.waitfor(); I don't understand why. Any refence to where I can get more information on this would be great or if you know how to go about this the proper way rather then my attempted way. Thanks for the help in advance. Jeff
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually this has come up before. The problem actually lies with javac.exe. Most executables will send the output to the stdout of the computer. javac actually sends all it's output (including exit level) to stderr. One way to correct your program is to eliminate the line
pro.waitFor();
then change the line
InputStream out = pro.getInputStream();
to
InputStream out = pro.getErrorStream();
Hope this helps
 
Jeff Holmes
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks once again, this worked great. - Jeff
 
Sean Li
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but I can get something from p.getInputStream(). It's exactely what I want but just slow.

I don't know whether the getErrorStream() can return the same thing. but I reckon I still need to wait until the process finished. am I right?
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sean Li:
but I can get something from p.getInputStream(). It's exactely what I want but just slow.

What is slow? The process that you create? There isn't much you can do about that other than work on the other process' code.

Originally posted by Sean Li:

I don't know whether the getErrorStream() can return the same thing. but I reckon I still need to wait until the process finished. am I right?


getErrorStream() returns the standard error stream, not standard out. They are two different things. One can read from the streams as the process executes. You do not have to wait until the process finishes. As a matter of fact, not reading from the streams can cause problems:
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.

java.lang.Process
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good RuntimeExec Information

Guy
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW: Your example was running Javac. I got curious and peeked into the source for Ant to find out how they run the compiler, and they use the tools jar from Java. It's technically not supported and Sun could make it go away in any future JDK, but it does work. I thought I read that JDK 6 will have new compiler APIs for writing IDEs or tools that need the AST or what have you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!