1

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?

Preferences screenshot

Tokra
  • 21
  • 5
  • rt.jar contains a lot of the basic content of Java. – Turtle Sep 07 '17 at 12:41
  • 1
    Possible duplicate of [Null Pointer Exception while using Java Compiler API](https://stackoverflow.com/questions/2543439/null-pointer-exception-while-using-java-compiler-api) – Joe Sep 07 '17 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 Sep 08 '17 at 09:20

2 Answers2

1

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 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 Sep 08 '17 at 08:21
  • I tried it with the jdk - without any success. The result is the same. – Tokra Sep 11 '17 at 11:10
0


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);
    }
Tokra
  • 21
  • 5