1

I am trying to compile and run java files from java code. I have a compiled java class and with this, try to compile java code. below is my code, but I don't see *.class file in either bin (in eclipse project out put folder) or in source place. Where has gone my *.class file if my compiler success. Or what is the wrong with my code? Trying in below 2 ways:

public class CompilerClass {  

    public static void main(String[] args) throws Exception {  

             Process p = Runtime.getRuntime().exec("javac com.java.Compileable.java");
             ProcessBuilder pb = new ProcessBuilder("javac", "com.java.Compileable.java");
    }  
}  
Chowdappa
  • 1,580
  • 1
  • 15
  • 31
  • If you're compiling with an IDE, what do the arguments say? If you're compiling from the command line, you *should* see the .class file in the same directory you compiled in (unless you're doing something a bit more fancy than `javac CompilerClass.java`). – Makoto Nov 15 '13 at 06:14
  • see there is nothing wrong with the way you do , but the API provides various feature already which you might have to understand or generate , thus this being an already existing solution , choice is yours – Hussain Akhtar Wahid 'Ghouri' Nov 15 '13 at 06:34
  • refer [this](http://stackoverflow.com/questions/10093290/run-a-java-file-using-processbuilder) – Hussain Akhtar Wahid 'Ghouri' Nov 15 '13 at 06:35

3 Answers3

2

well as an alternative you can use java compiler API

package javacompiler;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class COmpilerHello {
    public static void main(String[] args)
    {
        String s="C:/Users/MariaHussain/Desktop/hussi.java";
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        int result = compiler.run(System.in,System.out,System.err,s);
        System.out.println("Compile result code = " + result);
    }
}
0

Test it with file you have in the same directory as your CompilerClass

public static void main(String[] args) throws Exception {  

         Process p = Runtime.getRuntime().exec("javac SomeClass.java");
         ProcessBuilder pb = new ProcessBuilder("javac", "SomeClass.java");
}  

Works fine for me. SomeClass.java being in same directory as CompilerClass

Ran from command line

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

you have to state the location of the file. It should be something like this

public static void main(String[] args) throws Exception {  

         Process p = Runtime.getRuntime().exec("javac C://JavaProject//SomeClass.java");
         ProcessBuilder pb = new ProcessBuilder("javac", "C://JavaProject//SomeClass.java");
}
Jeremy
  • 641
  • 1
  • 7
  • 15
  • I tried this too. String classLoc ="C://Users//c0328502//Documents//Workspace//Java 1.7//package//com//java//Compileable.java"; Process p = Runtime.getRuntime().exec("javac "+classLoc); ProcessBuilder pb = new ProcessBuilder("javac", classLoc); – Chowdappa Nov 15 '13 at 06:36
  • uhmm i think you have to change directory to the directory that contains the .java file. or just can just put the jar file inside the directory that contain the .java file. this should work – Jeremy Nov 15 '13 at 06:43