Null Pointer Exception while using Java Compiler API

前端 未结 6 2325
北荒
北荒 2020-11-27 07:03

MyClass.java:

package test;
public class MyClass {
    public void myMethod(){
        System.out.println(\"My Method Called\");
    }
}

Li

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 07:54

    SUPPLEMENT: The answer above is to set the java.home system property within the program. That works for me too, but it's not a very general solution, since you've hard-coded for one jdk version. The alternative that I'm using now is to give the full path to "java" on the command line when I run the program. Such as (consistent with examples above):

    C:\\Program Files\Java\jdk1.7.0_02\jre\bin\java -cp ..\classes path.to.program.myProgram
    

    Giving the full path to the jdk version means that's the version that's running the program, so that's the one that will be fetched with ToolProvider.getSystemJavaCompiler();

提交回复
热议问题