sun.tools.javac.Main : Java Glossary

sun.tools.javac.Main
Sun’s undocumented and deprecated interface to programmatically invoke the javac.exe compiler as a class that runs in your own JVM (Java Virtual Machine). It is replaced by javax.tools. JavaCompiler.

In Java version 1.5-, you have only an undocumented and hence deprecated technique of invoking the javac.exe compiler programmatically.

You normally invoke javac.exe from the command line, but you can also invoke it from within a Java program. Use the sun.tools.javac.Main class located in jdk1.7.0_04/lib/tools.jar to pass it an array of Strings equivalent to the command line parameters. You might do this to bulk-compile large numbers of files without reloading the javac compiler for each file. Beware! The following example uses an undocumented, deprecated technique calling com.sun.tools.javac. Main. Normally you would use javax.tools. Here is how you use it:

This technique how ANT achieves its remarkable speed compiling large number of files. It does not keep loading java.exe over and over.

You too might use this technique for speed to avoid reloading javac.exe, or to have arbitrarily long command lines. You can also get long command lines with the @listOfFiles technique.

Capturing the error messages from javac.exe can be a hassle since they scroll off the screen faster than you can read them. Ordinary > redirection won’t capture them to a file because they are going to STDERR, not STDOUT. I use a program called tcc/TakeCommand to solve this problem. It lets you independently redirect STDOUT and STDERR. You can also use the -Xstdout to bypass the problem.

In Java version 1.6 or later programmatic invocation of the compiler will become an official interface. Read the JSR. For more details on how you do on the fly composing, compilation and execution see my sample code.


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/javacmain.html J:\mindprod\jgloss\javacmain.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.214]
You are visitor number 25,425.