0

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.

2 Answers2

2

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • So I have a question regarding this, if i were to create a jar executing use the Runtime.exec function to perform such task, would this ability work just as it would like through the CMD you posted? –  Feb 22 '12 at 21:58
  • @Legend: I don't know, to be honest. If it could find the right binary, then possibly... but it would be a slightly awkward bootstrap. – Jon Skeet Feb 22 '12 at 21:59
  • Ahh I see, is there anyway to make it execute as a file (.jar, .exe) and make it so that is does this with the compiler, I would prefer not to use a .bat though –  Feb 22 '12 at 22:01
  • @Legend: Any reason you don't just change the .jar association to use the JDK instead of the JRE? Or is this going to be distributed to other people, and you don't want to have to explain that? If so, you probably *will* want to add a jar file as a dependency - it's not sure what you mean by "without having it as a referenced library". – Jon Skeet Feb 22 '12 at 22:03
  • Well, it should only be around 33kb, it is 6.2mb with the reference library. Also, the JavaCompiler works fine within Eclipse, where I can specify to run with JDK. If this was possible in java, that would be perfect to answer number 1. –  Feb 22 '12 at 23:12
0

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

Community
  • 1
  • 1
Mutant Platypus
  • 145
  • 1
  • 8
  • All of the users will have JDK. Also, if I can find a way to get the File representation of the JDK directory, how could I use this to do what I want? –  Feb 23 '12 at 00:57
  • I know about the tools.jar, that is what is current. That is what makes it 6mb, instead of just 33kb. –  Feb 23 '12 at 00:59
  • @Legend Uhhm... in order to dynamically load a .jar file after searching for the JDK directory...I don't know. Try poking around System.load and System.loadLibrary – Mutant Platypus Feb 23 '12 at 01:06
  • Ok, to I messed around with the properties. System.getProperty("java.library.path").split(";")[0] returns the location of the JDK. Would you happen to know what DLL I need to load in order to specify JDK instead of JRE? –  Feb 23 '12 at 01:17
  • Ahh so once I have the class, I use reflection to get the method to return the compiler? Correct me if I'm wrong. –  Feb 23 '12 at 02:09
  • I think so. The only things I found were "use a custom class loader to load tools.jar" I'm not sure which class to load, though. I'm guessing com.sun.tools.javac.api.JavacTool – Mutant Platypus Feb 23 '12 at 02:12
  • Not sure if I did it wrong, but using reflection to call method create, you still need to reference the jar to cast it. –  Feb 23 '12 at 02:40