使用 Java Compiler API 时出现空指针异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2543439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Null Pointer Exception while using Java Compiler API
提问by java_geek
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. The ToolProvider.getSystemJavaCompiler() is returning null. Can someone tell me what is wrong with the code
我正在执行 SimpleCompileTest 类并获得 NullPointerException。ToolProvider.getSystemJavaCompiler() 返回 null。有人可以告诉我代码有什么问题吗
回答by rashid
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.NullPointerExceptionas 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.
我得到java.lang.NullPointerException了它所提到的。我用 打印出 Java 主目录System.out.println(System.getProperty("java.home"));,并注意到C:\Program Files\Java\jre7即使在我将首选项更改为使用 JDK1.7 而不是 JRE1.7 之后,我的 Eclipse 仍指向“ ”。
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.
回答by Jon Skeet
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 explicitlyspecifying the version of java.exe you're using as the one in your JDK directory.
运行时SimpleCompileTest,尝试明确指定您使用的 java.exe 版本作为 JDK 目录中的版本。
回答by Ravi Sharma
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.jarfound in Java\jdk1.8.0_31\libif you are using JDK. what i did was to go to project-->properties-->Java Build Path-->order and export-->and moved tool.jarto the top of other JRE and projects.
This helped me get rid of null hope this help you as well.Happy compiling...:-)
但是,问题很简单,因为如果您使用的是 JDK Tools.jar,Java\jdk1.8.0_31\lib则可以在 中找到此代码。我所做的是转到project-->properties-->Java Build Path-->order and export-->并移至tool.jar其他 JRE 和项目的顶部。这帮助我摆脱了 null 希望这对你也有帮助。编译愉快...:-)
回答by stacker
Probably you have a JRE instead of JDK installed. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6477844
可能您安装了 JRE 而不是 JDK。http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6477844
回答by aswininayak
Its working with Java application by expicitily including the tools.jar but for web application not working. Throwing Nullpointer
它通过明确地包含 tools.jar 来处理 Java 应用程序,但对于 Web 应用程序不起作用。抛出空指针
回答by Roger F. Gay
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):
补充:上面的答案是在程序中设置 java.home 系统属性。这也适用于我,但它不是一个非常通用的解决方案,因为您已经为一个 jdk 版本进行了硬编码。我现在使用的替代方法是在运行程序时在命令行上提供“java”的完整路径。例如(与上面的例子一致):
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(); 获取该版本;

