I read all the posts regarding this problem and no solution works for me, I get always null.
I use JRE and put the tools.jar in the lib directory, added it to the build path but when I want to jump to declaration Eclipse wants to jump into rt.jar (?) what I totally don't understand.
Could that be the reason that I get only null? How can I configure that correctly?
What are the criteria for getSystemJavaCompiler() to return null?
-
rt.jar contains a lot of the basic content of Java.Turtle– Turtle2017-09-07 12:41:42 +00:00Commented Sep 7, 2017 at 12:41
-
1Possible duplicate of Null Pointer Exception while using Java Compiler APIJoe– Joe2017-09-07 12:42:26 +00:00Commented Sep 7, 2017 at 12:42
-
I know that rt.jar contains the precompiled Java classes but it should not contain the content of the jdk. Is the class in rt.jar only an inoperable class only returning null?Tokra– Tokra2017-09-08 09:20:06 +00:00Commented Sep 8, 2017 at 9:20
Add a comment
|
2 Answers
JRE is the Java Runtime Environment. It doesn't have a compiler, and therefore you're getting a null. If you use a full-fledged JDK, you'd get a non-null result.
2 Comments
Tokra
Hi, thanks to the answer. Unfortunately I have no choice using jre instead of jdk. But I read that when I copy tools.jar in the lib path of the application or - another attempt - in the lib path of the jre installation I can use the compiler. I need this ability to make the strategy pattern more dynamical.so a new entry in a database is enough to extend the application.
Tokra
I tried it with the jdk - without any success. The result is the same.
I found a workaround for my problem. First I used the jre again. I put the tools.jar in the lib directory of the application.
ToolProvider.getSystemJavaCompiler() return null.
This is the workaround to get the JavaCompiler:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null){
try {
Class<?> javacTool = Class.forName("com.sun.tools.javac.api.JavacTool");
Method create = javacTool.getMethod("create");
compiler = (JavaCompiler) create.invoke(null);
} catch (Exception e) {
throw new AssertionError(e);
}