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");
    }  
}  
4

3 回答 3

2

作为替代方案,您可以使用 java 编译器 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);
    }
}
于 2013-11-15T06:25:21.377 回答
0

使用与您在同一目录中的文件进行测试CompilerClass

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

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

对我来说很好。SomeClass.java与在同一目录中CompilerClass

从命令行运行

于 2013-11-15T06:23:06.347 回答
0

您必须说明文件的位置。它应该是这样的

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");
}
于 2013-11-15T06:31:28.927 回答