0
public void actionPerformed(ActionEvent e)
    {
        String sout;
        try
          {         
            if(e.getSource()==compile)
            {
            sout=input.getText();
            Runtime rt=Runtime.getRuntime();                            
Process p=rt.exec("javac sout.java",null,new     File("C:/c/compile assign"));
            }           

I am storing the class name in String sout(through TextField) then calling sout.java (i.e classname.java) but it was unable to build a class file.

            if(e.getSource()==run)
            {
InputStream is = Runtime.getRuntime().exec("java input.getText()",null,new             File("C:/c/compile assign")).getInputStream();
            //BufferedInputStream b=new BufferedInputStream(is);
         BufferedReader br= new BufferedReader(new InputStreamReader(is));      
            output.setText(br.readLine()+"hello");
            }
        }
        catch(Exception e1)
        {
        e1.printStackTrace();
        }
    }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
tabish
  • 91
  • 1
  • 2
  • 10
  • Commands to `exec` or better `ProcessBuilder` are better kept in a `String[]`. Be sure to visit the Java World article linked from the [`exec` info. page](http://stackoverflow.com/tags/runtime.exec/info). Implement all the recommendations. – Andrew Thompson Sep 15 '12 at 11:59

1 Answers1

1

If you're keeping the classname in a string sout as supplied in the input textfield then your output compile command should be:

Process p=rt.exec("javac " + sout + ".java", null, new File("C:/c/compile assign"));

Also your run command should be:

Runtime.getRuntime().exec("java " + input.getText(), null, new File("C:/c/compile assign")).getInputStream();

You will probably want to add further validation before using the raw value (e.g. class exists) from this field before launching the java command.

Reimeus
  • 158,255
  • 15
  • 216
  • 276