0

I am working on web-app and i am using Runtime class to create process for executing the "javac" command for java files. The code i used is :

 try{
         String []cmdcom={"cmd.exe","cd..","javac",javafile};

         Runtime rt = Runtime.getRuntime();
         Process p = rt.exec(cmdcom);
         String line=null;
         BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
         line= br.readLine();
         StringBuffer sb =new StringBuffer();
         while(line!=null)
         {
             sb.append(line);
         }
         compileoutput=sb.toString();
         System.out.println(compileoutput);
    }

The cd command is used for going into the previous directory where my java file i.e "javafile" is stored.

So,I am having problem in the cmdcom array,how should i rewrite this command?

The execution code I used is:

  try{
    ProcessBuilder pb =new ProcessBuilder("java -cp . ",request.getParameter("filename"));
    pb=pb.directory(fl.getParentFile());
    Process p = pb.start();
    //Process p=Runtime.getRuntime().exec("java -cp . "+request.getParameter("filename"));
    p.waitFor();
    BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream())); 
    String line;
    StringBuffer sb=new StringBuffer();
    while((line=br.readLine())!=null)
    {
        sb.append(line+"\n");
    }
    line=null;
    line=sb.toString();
    Outmessage=Outmessage.concat(line);
    }
    catch(IOException ioe)
    {
       System.out.println(ioe.getMessage());
    }
Mayank
  • 19
  • 6
  • Like everybody else who tries this, use ProcessBuilder which will allow you to modify the execution context (directory) that the command should be executed in. Like [this](http://stackoverflow.com/questions/15218892/running-a-java-program-from-another-java-program/15220419#15220419) and [this](http://stackoverflow.com/questions/28955020/compiling-and-executing-using-exec-in-java-fails-using-command-that-works-from-t/28955036#28955036) – MadProgrammer Mar 27 '15 at 08:49
  • To compile Java code at runtime, you may want to use the [Compiler API](http://www.javabeat.net/the-java-6-0-compiler-api/). – JimmyB Mar 27 '15 at 08:50
  • Or you could use the inbuilt JavaCompiler class, demonstrated [here](http://stackoverflow.com/questions/21544446/how-do-you-dynamically-compile-and-load-external-java-classes/21544850#21544850) – MadProgrammer Mar 27 '15 at 08:54
  • 1
    You can’t execute multiple commands in one `exec` call. Use `ProcessBuilder` to run `javac` as you can [set the working directory](http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#directory(java.io.File)) before launching the program, therefore have no need for neither, `cd..` nor `cmd.exe`. Or just use the [compiler API](http://docs.oracle.com/javase/7/docs/api/javax/tools/package-summary.html)… – Holger Mar 27 '15 at 08:55
  • Thank you for suggesting the Compiler API. I have been searching for API like this for Compilation of java for my web app. – Mayank Mar 27 '15 at 08:56
  • Hey,I am using the Compiler API which saved me a lot of work for compiling java codes but I m facing problem in executing the java files.So,I used ProcessBuilder class to execute the java program but it also shows create process error, I am attaching the Execution code . – Mayank Mar 28 '15 at 05:34

0 Answers0