ToolProvider.getSystemJavaCompiler is returning null

I trying to write a library that will generate and compile code at runtime.

I have some alpha code that I can compile and load into a P5 sketch, but it fails because ToolProvider.getSystemJavaCompiler() is returning null.

Doing this should be possible (see http://forum.processing.org/two/discussion/82/dynamically-loading-java-source-files).

Can anyone suggest why I'm getting null from ToolProvider.getSystemJavaCompiler() ?

Thanks

Answers

  • It might be that you need a JDK installed, since the JRE probably does not include the compiler

  • Isn't Processing a JDK? It includes a compiler.

  • When you instal Java onto your computer you have 2 options either just the JRE (Java Runtime Environment) which is needed to run compiled programs and the JDK (Java Development Kit) which includes the JRE and compiler.

    Processing is not a JDK it is a programming environment that uses Java.

    My previous comment was just a possibility for you to look at.

    What is your OS and what version of Processing are you using?

    Can you create a small simple sketch that attempts to get the compiler but returns null that we can try on our machines? It doesn't need to compile anything so it so no extra files will be needed.

  • "Processing is not a JDK it is a programming environment that uses Java."

    How does Processing generate class files? Processing does not require that you also install a JDK.

    "When you install Java onto your computer you have 2 options either just the JRE (Java Runtime Environment) which is needed to run compiled programs and the JDK (Java Development Kit) which includes the JRE and compiler."

    I'm aware of the difference. Processing used to include "tools.jar" (part of the JRE). This is no longer the case. I believe this is because Oracle changed the terms for redistribution.

    Here's a simple sketch but this is not how I'm trying to implement this:

    import javax.tools.JavaCompiler;
    import javax.tools.ToolProvider;
    
    JavaCompiler compiler;
    
    void setup() {
        compiler = ToolProvider.getSystemJavaCompiler();
        if (compiler == null ) println("compiler is null!");
    }
    

    But I'm writing a library. it's straight-up Java code.

    https://gist.github.com/Neurogami/68399aac32bcd3097c5c

    The long-term goal is that the library generate Java code at runtime, compile it, load the resulting class file, and use it. I've hard-coded stuff just to see what works. (There's a way to do this in memory as well but the "write to disk" approach makes things easier to test ideas.)

    If I copy "tools.jar" (from my installed JRE) into my library's lib folder, and then set that location as java.home, I can get a reference to a compiler. (The path-setting is a hack; I at least need to see how to get the runtime location of the library and then construct a path to that lib folder.)

    The question might be better described as "How can a Processing library get access to Processing's compiling tools?" What files from a Processing installation do I need to include in my library?

    I'm on Ubuntu 10.10 using processing-2.2.1 x64, but I really am looking for a general approach so I can do this on other platforms.

  • OK I tried your code with PS2.2.1 and OSX and got null. Sorry but I don't know what the solution is. Did you try the code from the previous discussion?

  • "Did you try the code from the previous discussion?"

    No, I did not; it looked to be doing way more than what I wanted to do. I took at as an example of what might be possible in principle.

    However, I think I may have to give it a shot and see what happens.

    So far, fro assorted experiments and Googling, think I need to have my library include tools.jar (and perhaps some other parts of a JDK) and the code has to know where it lives so it can set the java.home value and use it'sown cllass/class files for compiling.

    I'm not super optimistic.

  • edited June 2015

    I assume that you are generating the source code for a single Java class on the fly then compiling that into a .class and finally creating instances of the Java class. Am I right?

    If so there is a way to generate the class file that does not require a Java compiler. It is what I did with my library, Jasmine.

  • I have code that sends OSC (Open Sound Control) messages. Since different programs expect different OSC messages I want a way for a user to edit a config file with some OSC message templates.

    Parsing these templates at runtime every time an OSC message needs to be sent will add too much latency. I've been thinking about some kind of caching system, but I think the fastest execution would come from code designed specifically for the given messages. The idea is to read in the config messages, generate the corresponding Java source code, compile it, and use it for the remainder of the program lifetime.

    Yes, Java bytecode is an ultimate goal but the prospect of writing code to generate bytecode is unpleasant. I first want to see if I can do standard source-code compilation.

Sign In or Register to comment.