Java 编译器 API NullPointerException
我正在使用此代码在运行时编译 Java 文件。首先,这是我的目录树(在 Eclipse 中)。
+---- src
+----- package
+------ Compile.java
+
+
+---- temp
+----- anotherpackage
+------ Temp.java (file to compile)
这是我收到 NullPointerException 的代码(我已经尝试使用 JDK 作为 Eclipse 中的标准 VM)。
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:
class CustomJavaFileObject extends SimpleJavaFileObject {
protected CustomJavaFileObject(URI uri, Kind kind) {
super(uri, kind);
}
}
我做错了什么?
编辑:
我的 PATH 中没有 JDK(并且我无法添加它)
这是我的堆栈跟踪:
java.lang.NullPointerException
at package.Compiler.compile(Compiler.java:20)
at package.Interactive.main(Interactive.java:19)
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)
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();
}
Here is the CustonJavaFileObject:
class CustomJavaFileObject extends SimpleJavaFileObject {
protected CustomJavaFileObject(URI uri, Kind kind) {
super(uri, kind);
}
}
What am I doing wrong?
EDIT:
I do not have the JDK in my PATH (and I can't add it)
Here is my stack trace:
java.lang.NullPointerException
at package.Compiler.compile(Compiler.java:20)
at package.Interactive.main(Interactive.java:19)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
导致问题。将您的 JRE 指向 JDK 内部,因为与 jdk 不同,jre 不提供任何工具。
causes the issue. Point your JRE to be inside the JDK as unlike jdk, jre does not provide any tools.