0

I'm developing java application which allow to white or paste a source-code and run it without doing compiling procedure manually.what it do is save java code from text-box to a text-file and execute cmd command java java-file after execute javac java-file.file.

it's work fine but this problem came across when java file doesn't have any output .i mean if code create a swing form ..cmd haven't any output.then my java program get stuck actually i can't close it.i have to use external program to close it like taskmanager. but when source-code has command-line output this problem never occurred .

OutputStream stdin = null;
BufferedReader br = null;
String line = null;
String command = null;

command = jTextField1.getText();

System.out.println(command);
Process p = Runtime.getRuntime().exec(command);
stdin = p.getOutputStream(); //use this to push commands

//processing stdout
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
// br.flush();  
byte[] bytes = new byte[4096];
///////////////////////////////////////////
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
while (in.read(bytes) != -1)
{
    jTextArea1.append(br.readLine()+"\n");
}
///////////////////////////////////////////
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • 2
    Essentially, you're blocking the Event Dispatching Thread, preventing it from updating the UI. Take a look at [this example](http://stackoverflow.com/questions/15396694/swing-message-doesnt-get-displayed-until-after-runtime-getruntime-exec-fini/15398441#15398441) and [this example](http://stackoverflow.com/questions/15801069/printing-a-java-inputstream-from-a-process/15801490#15801490) for more ideas... – MadProgrammer Mar 20 '14 at 05:09
  • Also, take a look at [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – MadProgrammer Mar 20 '14 at 05:23
  • Read (and implement) *all* the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to `exec` and build the `Process` using a `ProcessBuilder`. Also break a `String arg` into `String[] args` to account for arguments which themselves contain spaces. – Andrew Thompson Mar 20 '14 at 05:31
  • *"I'm developing java application which allow to white or paste a source-code and run it without doing compiling procedure manually"* Use the [`JavaCompiler`](http://docs.oracle.com/javase/7/docs/api/javax/tools/JavaCompiler.html) for this. It has many advantages over starting a new process, including better reporting and that it can *compile source in memory (**without** saving to disk first).* – Andrew Thompson Mar 20 '14 at 06:19

0 Answers0