0

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)
3
  • You should include the stack trace. Commented Sep 30, 2011 at 23:01
  • A compiler may not be available in your system. check the ToolProvider API and it will say that the getSystemJavaCompiler will return null if no compiler is provided. Commented Sep 30, 2011 at 23:01
  • @HovercraftFullOfEels The JDK is not in my PATH and I can't add it, but even when I use the JDK as my StandardVM it still doesn't work. Commented Sep 30, 2011 at 23:03

1 Answer 1

0
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 

causes the issue. Point your JRE to be inside the JDK as unlike jdk, jre does not provide any tools.

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

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.