Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Compiling with javac from within Java

Thread view: 
Roedy Green - 05 Nov 2005 17:37 GMT
After decompiling many undocumented classes, I finally discovered
a way to invoke Javac from within the JVM without using exec, running
the compiler inside the same JVM. This is MUCH faster than using JavaC
if you want to do repeat compiles. This is how ANT does it.

// compiling from within a JVM
// without spawning javac.exe or a separate JVM
// com.sun.tools.javac.Main lives in tools.jar
// Make sure it is on the classpath. It won't be by default.
import com.sun.tools.javac.Main;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

// ...

// simulate an arbitrarily long command line.
// No wildcards since no command line interpreter to expand them.
String[] optionsAndSources = { "-g", "-source", "1.5", "-target",
"1.5", "Apple.java", "Banana.java", "Cantaloup.java"};

// where Javac output goes
PrintWriter out = new PrintWriter( new FileWriter( "C:/temp/out.txt" )
);

// Compile all three sources at once
int status =  Main.compile( optionsAndSources, out );
System.out.println( "status: " + status );

For future reference, this is documented in the Java glossary under
javac.exe  at
http://mindprod.com/jgloss/javacexe.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Gordon Beaton - 05 Nov 2005 16:54 GMT
> After decompiling many undocumented classes, I finally discovered a
> way to invoke Javac from within the JVM without using exec, running
> the compiler inside the same JVM. This is MUCH faster than using
> JavaC if you want to do repeat compiles.

Congratulations. The technique is described by Sun in a TechTip from
July 2003:

 http://java.sun.com/developer/JDCTechTips/2003/tt0722.html

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.