简体   繁体   English

使用 Java Compiler API 时出现空指针异常

[英]Null Pointer Exception while using Java Compiler API

MyClass.java:我的类.java:

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

Listing for SimpleCompileTest.java that compiles the MyClass.java file.编译 MyClass.java 文件的 SimpleCompileTest.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, null, fileToCompile);
        if(compilationResult == 0){
            System.out.println("Compilation is successful");
        }else{
            System.out.println("Compilation Failed");
        }
    }
}

I am executing the SimpleCompileTest class and getting a NullPointerException.我正在执行 SimpleCompileTest 类并获得 NullPointerException。 The ToolProvider.getSystemJavaCompiler() is returning null. ToolProvider.getSystemJavaCompiler() 返回 null。 Can someone tell me what is wrong with the code有人可以告诉我代码有什么问题吗

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 .我在Compile Java Files At Runtime玩弄源代码。

I was getting java.lang.NullPointerException as it is mentioned.正如提到的那样,我收到了java.lang.NullPointerException I printed out the Java home directory with System.out.println(System.getProperty("java.home"));我用System.out.println(System.getProperty("java.home"));打印出 Java 主目录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. ,并注意到即使在我将首选项更改为使用 JDK1.7 而不是 JRE1.7 之后,我的 Eclipse 仍指向“ C:\\Program Files\\Java\\jre7 ”。

I found a workaround by forcing the usage of JDK1.7 by setting system property like this:我通过设置如下系统属性来强制使用 JDK1.7,找到了一种解决方法:

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

Then I compiled my program and did not get any NullPointerException .然后我编译了我的程序并且没有得到任何NullPointerException

I suspect you're running into this problem - running the code with a JRE instead of a JDK.我怀疑您遇到了这个问题- 使用 JRE 而不是 JDK 运行代码。

When you run SimpleCompileTest , try explicitly specifying the version of java.exe you're using as the one in your JDK directory.当您运行SimpleCompileTest ,尝试明确指定您使用的 java.exe 版本作为 JDK 目录中的版本。

Probably you have a JRE instead of JDK installed.可能您安装了 JRE 而不是 JDK。 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6477844 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6477844

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.但是,问题很简单,因为如果您使用的是 JDK,则此代码位于Java\\jdk1.8.0_31\\lib中的Tools.jar中。 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.我所做的是转到project-->properties-->Java Build Path-->order and export-->并将tool.jar移动到其他 JRE 和项目的顶部。 This helped me get rid of null hope this help you as well.Happy compiling...:-)这帮助我摆脱了 null 希望这对你也有帮助。编译愉快...:-)

SUPPLEMENT: The answer above is to set the java.home system property within the program.补充:上面的答案是在程序中设置 java.home 系统属性。 That works for me too, but it's not a very general solution, since you've hard-coded for one jdk version.这也适用于我,但它不是一个非常通用的解决方案,因为您已经为一个 jdk 版本进行了硬编码。 The alternative that I'm using now is to give the full path to "java" on the command line when I run the program.我现在使用的另一种方法是在运行程序时在命令行上提供“java”的完整路径。 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();提供 jdk 版本的完整路径意味着这是运行程序的版本,因此将使用 ToolProvider.getSystemJavaCompiler(); 获取该版本;

Its working with Java application by expicitily including the tools.jar but for web application not working.它通过明确地包含 tools.jar 来处理 Java 应用程序,但对于 Web 应用程序不起作用。 Throwing Nullpointer抛出空指针

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM