Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java compiler at Runtime

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.

like image 234
Dipmedeep Avatar asked Aug 21 '10 11:08

Dipmedeep


People also ask

Is Java compiled at runtime?

In Java, programs are not compiled into executable files; they are compiled into bytecode (as discussed earlier), which the JVM (Java Virtual Machine) then executes at runtime. Java source code is compiled into bytecode when we use the javac compiler.

Can you compile Java with JRE?

No you can't develop java programs only with JRE. You will need JDK for compiling your programs. JRE provides only runtime environment,but JDK is something you will need to compile your code to make them executable by your JRE .

What happens at run time in Java?

Runtime is the final phase of the program lifecycle in which the machine executes the program's code. When the source code of the program is being edited. This phase includes bug fixing, refactoring, and adding new features.

How do I run a Java runtime file?

C++ ProgrammingType 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.


2 Answers

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.

like image 136
josefx Avatar answered Oct 05 '22 11:10

josefx


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.

  • GCJ, a part of gcc which compiles C, Fortran, Pascal and other programming languages besides Java. It can also generate native code using the back-end of gcc. http://en.wikipedia.org/wiki/GNU_Compiler_for_Java

  • ECJ, the Eclipse Compiler for Java, is an open source incremental compiler used by the Eclipse JDT.

  • http://en.wikipedia.org/wiki/Jikes (This one doesn't support Java 6 and limited support for Java5)

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

like image 27
YoK Avatar answered Oct 05 '22 11:10

YoK