JavaCompiler : Java Glossary

JavaCompiler
javax.tools.JavaCompiler is aJava version 1.6 or later interface that lets your program fire up the Java compiler, (usually javac.exe) and compile Java source, all under your program’s control, in a platform-independent way.

You don’t use exec or sun.tools.javac. Main any more.

Benefits Learning More
Sample Code Links
sun.tools.javac.Main

Benefits

The advantages of the JavaCompiler approach are:

Sample Code

JavaCompiler’s extreme generality and total lack of documentation makes it unusually inscrutable. The following example generates a Java source program on the fly in RAM, (It does not write it to disk.) and uses the default JavaFileManager to generate the class file in the CWD (Current Working Directory)
Note: the Hypotenuse.class file, that is dynamically generated by TestJavaCompiler in the CWD, must be accessible via the classpath to be able to load. For example, if the CWD is E:\myprojects\com\mindprod\example, then E:\myprojects must be on the classpath.
Here is what the dynamically generated Hypotenuse.java file looks like:

sun.tools.javac.Main

InJava version 1.5- you have the deprecated, undocumented sun.tools.javac.Main to fire up the Javac.exe compiler to run in the same JVM and compile under your control.

Learning More

Oracle’s Javadoc on JavaCompiler class : available:
Oracle’s Javadoc on JavaFileObject class : available:
Oracle’s Javadoc on ForwardingJavaFileObject class : available:
Oracle’s Javadoc on SimpleJavaFileObject class : available:
Oracle’s Javadoc on JavaFileManager class : available:
Oracle’s Javadoc on ToolProvider class : available:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/javacompiler.html J:\mindprod\jgloss\javacompiler.html
logofeedback Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email If you want your message kept confidential, not considered for posting, please explicitly specify that.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.213]
You are visitor number 12,441.