user1181445
user1181445

Reputation:

JavaCompiler returning null

I am creating a program along the lines of Codingbat.com.

During Runtime, it needs to compile code, and then execute it. This has all been handled. Currently, I am forced to use the JavacTool, which requires it to be packed alongside.

I have 2 basic questions:

1) How can I stop the ToolProvider.getSystemJavaCompiler() from returning null when ran from an executable jar?

2) If the above is not possible, is there a way to add the jar of com.sun.tools.javac.api.JavacTool; without having it as a referenced library, so that it acts like a regular import?

Thanks for answering this, if you would like, I could upload the Jar with the referenced library, and the jar without it.

Just to be clear, the one with the referenced library works, but it is way to large, and slower then the jar that is ran through eclipse, that uses the JavaCompiler, not the JavacTool

Thanks

Edit:

I am pretty sure this is possible with java as I have seen it before, yet forget where and how.

Upvotes: 0

Views: 2022

Answers (2)

Mutant Platypus
Mutant Platypus

Reputation: 145

So you already know that all your users will have a JDK installed and you know how to find the classpath of the JDK when you're in your Java program. You don't need to load a DLL. You need to load the com.sun.tools.javac.api.JavacTool class in tools.jar. See How to load a jar file at runtime on how to load tools.jar

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500505

I suspect it's just a problem of which version of Java you run. If you run the version which comes with the JRE, it won't have the tools available. If you run the version which comes with the JDK, it will.

As an example, here's a short but complete program:

import javax.tools.*;

public class Test
{
    public static void main(String[] args)
    {
        System.out.println(ToolProvider.getSystemJavaCompiler());
    }
}

Running it with the JRE version of java.exe on my laptop:

c:\Users\Jon\Test>"\Program Files\java\jre7"\bin\java Test
null

And now with the JDK:

c:\Users\Jon\Test>"\Program Files\Java\jdk1.7.0"\bin\java Test
com.sun.tools.javac.api.JavacTool@441944ae

So try explicitly specifying the a Java binary associated with the JDK.

Upvotes: 2

Related Questions