MyClass.
java:
package test;
public class MyClass {
public void myMethod(){
System.out.println("My Method Called");
}
}
列出用于编译MyClass.java文件的SimpleCompileTest.java.
SimpleCompileTest.java:
package test;
import javax.tools.*;
public class SimpleCompileTest {
public static void main(String[] args) {
String fileToCompile = "test" + java.io.File.separator +"MyClass.java";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilationResult = compiler.run(null,null,fileToCompile);
if(compilationResult == 0){
System.out.println("Compilation is successful");
}else{
System.out.println("Compilation Failed");
}
}
}
我正在执行SimpleCompileTest类并获得NullPointerException. ToolProvider.getSystemJavaCompiler()返回null.有人可以告诉我代码有什么问题吗?