10

Hello All I am writing some software that will allow users to create their own Java classes for a specific use in my software package. Obviously, my software will need to be able to invoke a Java compiler to compile the user-generated classes for use in my program. However, I do not want to require users to download and install the entire JDK just so they can have access to the javac Java compiler. I understand that in Jave 6 there is a new Java Compiler API but even then, users with just the JRE and not the JDK will get a null object when they try to instantiate the Java compiler tool.

So, what is the best way to enable my program to compile Java classes while requiring the end user to just have the JRE installed on their machines? If this is not possible, what is the minimal set of libraries/jar files I would need to install on the user machine?

I suppose another possibility might be to use JWS (javaws) to launch the app over the web. In this case, is it possible for my software to not require the JDK (mainly tools.jar I think)? Would I somehow have to bundle tools.jar with my software?

4 Answers 4

6

You can use standalone Java compiler from Eclipse. It doesn't need JDK installed, and it's single JAR file you can use in your project. This is the compiler that is used inside Eclipse itself, but it can be integrated into other programs too. You can find current latest stable version at http://download.eclipse.org/eclipse/downloads/drops/R-3.6.2-201102101200/index.php, look for Look for JDT Core Batch Compiler. Documentation is available at http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_api_compile.htm

You can also use Janino, which can compile also smaller units than full Java classes: blocks, expressions. It's not full compiler though, it does not support all features of Java language that your users use (generics, annotations, enums, ...)

1
  • 1
    java -jar ecj-3.6.2.jar -d gaevfs.bin\ gaevfs.src\com\newatlanta\commons\vfs\provider\gae\GaeFileObject.java -cp D:\ProgXP\jre6\lib\rt.jar;D:\WorkSpace\diyism.com.war\WEB-INF\lib\appengine-api-1.0-sdk-1.5.5.jar;D:\WorkSpace\diyism.com.war\WEB-INF\lib\commons-vfs-2.0-SNAPSHOT.jar;D:\WorkSpace\diyism.com.war\WEB-INF\lib\gaevfs-0.3.jar;D:\WorkSpace\diyism.com.war\WEB-INF\lib\commons-logging-1.1.1.jar
    – diyism
    Nov 13, 2011 at 13:06
3

To use the java compiler, you need to include tools.jar into your application (e.g. it has to be reachable by the classloader who wants to load the compiler - most easily by the System class loader).

3
  • 2
    "..you need to include tools.jar into your application.." I was musing over this possibility when developing the SSCCE Text Based Compiler. As I mention in the help "..it probably violates Sun's distribution licence..". I realize people here are 'not lawyers', but if anybody has alternate theories/interpretations of the license, I'd love to hear them. Apr 8, 2011 at 21:33
  • 2
    Take the one from OpenJDK, and add a link to the source, then. Apr 8, 2011 at 21:46
  • 2
    Did not think of that! It neatly side-steps the 'license problem'. :) Apr 8, 2011 at 22:02
1

Maybe http://asm.ow2.org/ this will be usefull? To generate bytecode on fly?

0

It sounds like a better solution would be to use some scripting language that can run within the JVM without having to be compiled.

Possible the Java Scripting API would be of use to you. I'm sure there are other options as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.