JavaCompiler : Java Glossary

JavaCompiler
javax.tools.JavaCompiler is a Java 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

In Java 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
logo
Please email your , 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, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.210]
You are visitor number 13,375.