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

3
  • rt.jar contains a lot of the basic content of Java. Commented Sep 7, 2017 at 12:41
  • 1
    Possible duplicate of Null Pointer Exception while using Java Compiler API Commented 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? Commented Sep 8, 2017 at 9:20

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
I tried it with the jdk - without any success. The result is the same.
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);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.