Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Compiling with javac from within Java

Reply
Thread Tools

Compiling with javac from within Java

 
 
Gordon Beaton
Guest
Posts: n/a
 
      11-05-2005
On Sat, 05 Nov 2005 16:37:32 GMT, Roedy Green wrote:
> 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/JDCTec...03/tt0722.html

/gordon

--
[ 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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      11-05-2005
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
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there an option for auto-create output dirs when compiling with javac? Catherina Svensson Java 0 04-29-2008 06:09 AM
How to javac a java program w/ another java program which is w/o a main method cjeffwang@yahoo.com Java 1 10-31-2005 04:25 AM
Compiling java code from within Java John Morey Java 6 11-24-2004 10:33 AM
Using com.sun.tools.javac.Main() for compiling Jesper Sahner Java 7 11-21-2004 04:02 PM
Compiling Java code using Ant within Eclipse junk@andy-coleman.co.uk Java 1 11-05-2004 10:25 AM



Advertisments