使用 Java 编译器 API 时出现空指针异常
MyClass.java:
package test;
public class MyClass {
public void myMethod(){
System.out.println("My Method Called");
}
}
编译 MyClass.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");
}
}
}
我正在执行 SimpleCompileTest 类并收到 NullPointerException。 ToolProvider.getSystemJavaCompiler() 返回 null。有人可以告诉我代码有什么问题吗
MyClass.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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
可能您安装的是 JRE 而不是 JDK。 https://bugs.java.com/bugdatabase/view_bug?bug_id=6477844
Probably you have a JRE instead of JDK installed. https://bugs.java.com/bugdatabase/view_bug?bug_id=6477844
我遇到了同样的问题,
返回 null。即使使用后也
返回 null。
不过问题很简单,因为如果您使用的是 JDK,此代码位于
Java\jdk1.8.0_31\lib
中的Tools.jar
中。我所做的是转到project-->properties-->Java Build Path-->order and export-->
并移动tool.jar
到其他 JRE 和项目的顶部。这帮助我摆脱了空希望这也对你有帮助。编译快乐...:-)
I was having the same problem
was returning null. Even after using
was returning null.
However the issue was simple as this code is in the
Tools.jar
found inJava\jdk1.8.0_31\lib
if you are using JDK. what i did was to go toproject-->properties-->Java Build Path-->order and export-->
and movedtool.jar
to the top of other JRE and projects.This helped me get rid of null hope this help you as well.Happy compiling...:-)
它通过明确包含tools.jar 与Java 应用程序一起工作,但对于Web 应用程序不起作用。抛出空指针
Its working with Java application by expicitily including the tools.jar but for web application not working. Throwing Nullpointer
补充:上面的答案是在程序中设置 java.home 系统属性。这也适用于我,但这不是一个非常通用的解决方案,因为您已经为一个 jdk 版本进行了硬编码。我现在使用的替代方法是在运行程序时在命令行上提供“java”的完整路径。例如(与上面的示例一致):
给出jdk版本的完整路径意味着这是正在运行程序的版本,因此这就是将使用 ToolProvider.getSystemJavaCompiler() 获取的版本;
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):
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();
我遇到了同样的错误。也许我来不及回答这个问题,但我分享自己的经验,这可能会帮助其他人将来面临同样的问题。我正在玩编译的源代码运行时的 Java 文件。
正如提到的那样,我收到了
java.lang.NullPointerException
。我使用 System.out.println(System.getProperty("java.home")); 打印出 Java 主目录,并注意到我的 Eclipse 指向“C:\Program Files” \Java\jre7”,即使我更改了我的偏好以使用 JDK1.7 而不是 JRE1.7。我找到了一个解决方法,通过设置系统属性来强制使用 JDK1.7,如下所示:
然后我编译了我的程序,但没有得到任何
NullPointerException
。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 withSystem.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:
Then I compiled my program and did not get any
NullPointerException
.我怀疑您遇到了此问题 - 使用 JRE 运行代码而不是 JDK。
当您运行
SimpleCompileTest
时,请尝试显式指定您正在使用的 java.exe 版本作为 JDK 目录中的版本。I suspect you're running into this problem - running the code with a JRE instead of a JDK.
When you run
SimpleCompileTest
, try explicitly specifying the version of java.exe you're using as the one in your JDK directory.