Null Pointer Exception while using Java Compiler API

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

    I was having the same problem

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    

    was returning null. Even after using

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

    was returning null.

    However the issue was simple as this code is in the Tools.jar found in Java\jdk1.8.0_31\lib if you are using JDK. what i did was to go to project-->properties-->Java Build Path-->order and export--> and moved tool.jar to the top of other JRE and projects. This helped me get rid of null hope this help you as well.Happy compiling...:-)

提交回复
热议问题