7

I want to use JavaCompiler to dynamically create some classes.

I found the source code of the javax.tools package, but there is no implementation; some posts on the internet say it depends on tools.jar, I am not sure tools.jar associates with JRE.

So, can I run the program in a JRE environment without JDK installed?

Another question, what is the implementation detail of JavaCompiler, is it creating a new process to invoke the javac command?

thanks

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
Brodie
  • 605
  • 1
  • 9
  • 17

1 Answers1

10

JRE's need to include the interfaces, and classes in that package, but do not need to provide implementations.

Sun's JRE doesn't, and returns null from calling ToolProvider.getSystemJavaCompiler().

This other question addresses java 5.

Here's a blog post by Morten Nobel-Jørgensen on how to use eclipse's compiler as an implementation of javax.tools.JavaCompiler.

JavaCompiler javac = new EclipseCompiler();
Community
  • 1
  • 1
Stephen Denne
  • 36,219
  • 10
  • 45
  • 60
  • thank you, i already finished the coding, but if the getSystemJavaCompiler() returns null, the program would fail. so i need to avoid it – Brodie Nov 11 '09 at 09:43
  • The blog post link is dead, the updated link is [here](http://blog.nobel-joergensen.com/2008/07/16/using-eclipse-compiler-to-create-dynamic-java-objects-2/) – prunge Sep 06 '11 at 04:10