Java compiler at Runtime
Asked Answered
T

6

7

In my current project, I need to compile java code at runtime (in the background to process input from the user). This works fine with tools.jar in the classpath. However, not all users of my program have JDK installed on their system. Some of them only have JRE and in that case there is no java compiler available at runtime. I can solve that problem by including tools.jar from Sun as a part of my tool.

But tools.jar is very big (>12 MB). The problem is that I have to include the large jar file, although I am interested only in a small fraction of the functionality provided by this jar.

  1. Is it possible to break up the tools.jar file so that I have a small subset of classes that are required for compiling java code only?

  2. Is this illegal?

Thanks a lot.

Thrashing answered 21/8, 2010 at 11:24 Comment(0)
O
3

The Eclipse compiler is only 1.6 MB and should work without Eclipse. You can download it here. Also it looks like it implements the JavaCompiler API.

It is licensed under the Eclipse public license so including it in your own application should be no problem.

Omophagia answered 21/8, 2010 at 11:53 Comment(1)
This is an option I considered using. Yes, it is possible to use the Eclipse compiler without Eclipse. I had some problems using it because of its thread caching behavior, i.e., it is not possible to get more than one instance of the compiler from one thread. In my usage scenario, I am having trouble keeping track of the diagnostic messages from the compiler because I need a new instance of the compiler each time.Thrashing
B
1

I don't think it would be possible to breakup tools.jar, And also it should not be legal to include tools.jar.

Check http://forums.sun.com/thread.jspa?threadID=5161541

You could look for some 3rd party Java Compiler and change your code to use same.

But I don't know exact code for compiling using these.

Blowup answered 21/8, 2010 at 11:48 Comment(1)
GCJ is useless. Jikes is promising but doesn't support many Java 5 features, which is what most of us useClotho
L
1

Instead of compiling code at runtime, I would rewrite the code in a scripting language and use the scripting framework. There's no need then for the jdk and there are no legal issues to get around. Then you also have many choices of languages to use such as Beanshell, JavaScript, or jython.

Lour answered 21/8, 2010 at 12:9 Comment(1)
I've used the built in Rhino implmentation myself, and it does work great. It also has the bonus of a few built in functions that never made it into Rhino, as well as simplifying the whole process.Clotho
C
1

Probably a better question is why?

If you need to do dynamic math calculations, then consider JEval. If you need people to write simple plugins, consider some of the dynamic languages like Javascript or Python that @Jay mentioned.

For anything else, you should require that your user download the JDK, since thats what writing Java requires. It would be hard to find a good reason why you would want to embed a compiler into your program

Clotho answered 21/8, 2010 at 13:48 Comment(0)
S
0

do you want something like java-scripting like BeanShell? It help run java code without compile them.

Simone answered 21/8, 2010 at 12:0 Comment(0)
V
0

You may want to have a look at the Javassist library which contains a snippet compiler suitable for creating a method to do a calculation and then using that method.

Which one is most suitable depends on your actual needs - especially how frequently you need to do this.

Veda answered 21/8, 2010 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.