使用 Java 编译器 API 时出现空指针异常

发布于 2024-08-27 05:57:36 字数 905 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

帥小哥 2024-09-03 05:57:37

可能您安装的是 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

十年九夏 2024-09-03 05:57:37

我遇到了同样的问题,

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

返回 null。即使使用后也

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

返回 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

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...:-)

爱,才寂寞 2024-09-03 05:57:37

它通过明确包含tools.jar 与Java 应用程序一起工作,但对于Web 应用程序不起作用。抛出空指针

Its working with Java application by expicitily including the tools.jar but for web application not working. Throwing Nullpointer

阳光下慵懒的猫 2024-09-03 05:57:37

补充:上面的答案是在程序中设置 java.home 系统属性。这也适用于我,但这不是一个非常通用的解决方案,因为您已经为一个 jdk 版本进行了硬编码。我现在使用的替代方法是在运行程序时在命令行上提供“java”的完整路径。例如(与上面的示例一致):

C:\\Program Files\Java\jdk1.7.0_02\jre\bin\java -cp ..\classes path.to.program.myProgram

给出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):

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();

时光病人 2024-09-03 05:57:36

我遇到了同样的错误。也许我来不及回答这个问题,但我分享自己的经验,这可能会帮助其他人将来面临同样的问题。我正在玩编译的源代码运行时的 Java 文件

正如提到的那样,我收到了 java.lang.NullPointerException 。我使用 System.out.println(System.getProperty("java.home")); 打印出 Java 主目录,并注意到我的 Eclipse 指向“C:\Program Files” \Java\jre7”,即使我更改了我的偏好以使用 JDK1.7 而不是 JRE1.7。

我找到了一个解决方法,通过设置系统属性来强制使用 JDK1.7,如下所示:

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

然后我编译了我的程序,但没有得到任何 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 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.

我的影子我的梦 2024-09-03 05:57:36

我怀疑您遇到了此问题 - 使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文