Java编译器API NullPointerException [英] Java Compiler API NullPointerException

查看:329
本文介绍了Java编译器API NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个代码在运行时编译一个Java文件。首先,这是我的目录树(在Eclipse中)。

I am using this code to compile a Java file at runtime. First of all, here is my directory tree (in Eclipse).

+---- src
+----- package
+------ Compile.java
+
+
+---- temp
+----- anotherpackage
+------ Temp.java (file to compile)

我的代码中我得到NullPointerException(我已经尝试使用JDK作为我的标准VM在Eclipse中)。

Here is my code where I am getting the NullPointerException (I already tried using JDK as my Standard VM in Eclipse).

public static void compile(URI path, InputStream is, OutputStream os, OutputStream err) throws IOException {
    SimpleJavaFileObject source = new CustomJavaFileObject(path, Kind.SOURCE);
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    JavaCompiler.CompilationTask task = compiler.getTask(new PrintWriter(err), null, null, null, null, Arrays.asList(source));
    task.call();
}

这里是CustonJavaFileObject:

Here is the CustonJavaFileObject:

class CustomJavaFileObject extends SimpleJavaFileObject {
    protected CustomJavaFileObject(URI uri, Kind kind) {
        super(uri, kind);
    }
}

我做错了什么?

EDIT:

我在我的PATH中没有JDK it)

I do not have the JDK in my PATH (and I can't add it)

这是我的堆栈跟踪:

java.lang.NullPointerException 
  at package.Compiler.compile(Compiler.java:20)
  at package.Interactive.main(Interactive.java:19)


推荐答案

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 

会导致问题。指向你的JRE在JDK内部,与jdk不同,jre不提供任何工具。

causes the issue. Point your JRE to be inside the JDK as unlike jdk, jre does not provide any tools.

这篇关于Java编译器API NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆