toolprovider.getsystemjavacompiler() returns null

1k views Asked by At

First, I am seeing a lot of questions about the use of the JavaCompilerAPI, I want to clarify that I am creating an on-line simulation builder that takes too many inputs from the user to precreate classes. That is why I am using a java compiler in order to write the classes using the user's inputs.

As for my problem, I have tested with some basic compiler programs, and am presently working of code found here: Dynamic Compiling Without Create Physical File

The compilation of the code is successful, however when I run the code,
ToolProvider.getSystemJavaCompiler(); returns null.

From other entries I understand one cause might be that the default java.home is JRE, so I added the line where I set java home to my JDK version:

System.setProperty("java.home", "C:\\Program Files (x86)\\Java\\jdk1.7.0_51;");

I have also added tools.jar to the folder with my program, and called the program specifying tools.jar in the classpath like so:

java -cp ".;tools.jar" Compiler

These approaches have not changed anything. Any ideas about what might be the problem?

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.*;

public class Compiler {

    static final Logger logger = Logger.getLogger(Compiler.class.getName());
        static String sourceCode = "class HelloWorld{"
        + "public static void main (String args[]){"
        + "System.out.println (\"Hello, dynamic compilation world!\");"
        + "}"
        + "}";

    public void doCompilation() {
        System.out.println(System.getProperty("java.home"));
        System.setProperty("java.home", "C:\\Program Files (x86)\\Java\\jdk1.7.0_51;");
        System.out.println(System.getProperty("java.home"));
        SimpleJavaFileObject fileObject = new DynamicJavaSourceCodeObject("HelloWorld",sourceCode);
        JavaFileObject javaFileObjects[] = new JavaFileObject[]{fileObject};

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        System.out.println(compiler);

        StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, Locale.getDefault(), null);

...

0

There are 0 answers