Close
jGuru Forums
Posted By: Florian_Kummer Posted On: Monday, August 26, 2002 07:16 AM
Hello; I want to invoke the javac Compiler from within a running java application:
Process javac = Runtime.getRuntime().exec ( "javac tst.java"); javac.waitFor(); // causes the current thread to wait until javac is finished
Re: invoking javac with Runtime.exec(...)
Posted By: Axel_Richter Posted On: Wednesday, August 28, 2002 11:26 PM
class FileCompileAction extends AbstractAction { public FileCompileAction() { super("Compile"); } public void actionPerformed(ActionEvent e) { String cmd1 = "Javac"; String cmd2 = ""; String fName = "Main.java"; File dir = new File("D:\"); String[] cmdarray = {cmd1, cmd2, fName}; String[] env = {}; try { Process p = Runtime.getRuntime().exec(cmdarray, env, dir); BufferedInputStream err = new BufferedInputStream(p.getErrorStream()); BufferedInputStream out = new BufferedInputStream(p.getInputStream()); String cerr = ""; String cout = ""; Character c; int i = -1; while((i = err.read()) != -1) { c = new Character((char)i); cerr = cerr.concat(c.toString()); } err.close(); i = -1; while((i = out.read()) != -1) { c = new Character((char)i); cout = cout.concat(c.toString()); } out.close(); int stat = 0; try { stat = p.waitFor(); } catch (InterruptedException ie) { ie.printStackTrace(); } p.destroy(); System.out.println("Ende mit: "+stat); System.out.println(cout); System.out.println(cerr); } catch (IOException ioe) { ioe.printStackTrace(); } } }
Posted By: Anonymous Posted On: Tuesday, August 27, 2002 02:42 AM