Null Pointer Exception while using Java Compiler API

前端 未结 6 2329
北荒
北荒 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:43

    I got the same error. Maybe I am too late to answer this question, but I share my own experiences, it might help someone else facing the same issue in the future. I was playing around with the source code at Compile Java Files At Runtime.

    I was getting java.lang.NullPointerException as it is mentioned. I printed out the Java home directory with System.out.println(System.getProperty("java.home"));, and noticed my Eclipse was pointing to "C:\Program Files\Java\jre7" even after I changed my preferences to use JDK1.7 instead of JRE1.7.

    I found a workaround by forcing the usage of JDK1.7 by setting system property like this:

    System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_02");
    

    Then I compiled my program and did not get any NullPointerException.

提交回复
热议问题