trung / InMemoryJavaCompiler

Utility class to compile java source code in memory
Apache License 2.0
267 stars 85 forks source link

NullPointerException on any use #11

Closed NonameSLdev closed 6 years ago

NonameSLdev commented 7 years ago

Hey, I'm getting a NullPointerException whenever I try to use the compiler. I tried it with your example:

StringBuffer sourceCode = new StringBuffer();
sourceCode.append("package org.mdkt;\n");
sourceCode.append("public class HelloClass {\n");
sourceCode.append("   public String hello() { return \"hello\"; }");
sourceCode.append("}");

try{
        Class<?> helloClass = InMemoryJavaCompiler.compile("org.mdkt.HelloClass", sourceCode.toString()); // <--- Line 16
    }catch(Exception e){
        e.printStackTrace();
    }

And it doesn't work. The exception:

java.lang.NullPointerException
    at org.mdkt.compiler.InMemoryJavaCompiler.compile(InMemoryJavaCompiler.java:19)
    at me.nonamesldev.javacompilerexample.JavaCompilerExample.main(JavaCompilerExample.java:16)

I tried it with different source codes, all don't work.

NonameSLdev commented 7 years ago

After a little debugging, I found out that in the InMemoryJavaCompiler class, javac is null for a reason - I didn't have JDK installed. Silly me :P Add a note in the readme.md that you need to compile the project with JDK and that the client also needs JDK :)

shivam92 commented 7 years ago

I have installed JDK, even I am getting NullPointerException.When I debug I got javac is null. I configure the application in eclipse.

static JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); is not able to create instance of javac If any idea please provide me.

trung commented 6 years ago

Related to https://github.com/trung/InMemoryJavaCompiler/issues/12