|
|
Compiling with javac from within Java
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.
|
|
0
|
|
|
|
Reply
|
my_email_is_posted_on_my_website (4730)
|
11/5/2005 4:37:32 PM |
|
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/JDCTechTips/2003/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
|
|
0
|
|
|
|
Reply
|
not108 (1060)
|
11/5/2005 3:54:38 PM
|
|
|
1 Replies
46 Views
(page loaded in 0.068 seconds)
Similiar Articles: Jdk 1.6 will not compile: cannot access java.lang.Object - comp ...Jdk 1.6 will not compile: cannot access java.lang.Object - comp ...:)Your OS runs old javac; correct your PATH env. var ... 0_04 just came out so you might as well use ... class file has wrong version 49.0, should be 48.0 - comp.lang.java ...Compile with jdk 1.5, but use the switch "-target 1.4" http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javac.html Also look at the cross-compilation example at the ... "Platform default encoding" - comp.lang.java.helpI'm not sure which comp.lang.java.* newsgroup is appropriate. javac documentation at (inter alia ... from the above, but I'd prefer an explicit statement about compile ... Programming using JSP and Tomcat: cannot be resolved to a type ...... org.apache.jasper.JasperException: Unable to compile class ... page import="java.net.*, java.io.*, java.sql.*, java ... It's a bit mysterious, but it seems javac with the -d ... Checksum with no creation date - comp.lang.java.help... test: - Generate a jar file from some java ... matter, but if there were a bug in Javac or a new optimisation, it might. Calling MATLAB from Visual Studio in a C program - comp.soft-sys ...> > Compiling from the command-line or within Matlab are not options, > since I need to debug the C ... calling a VB6 .DLL from java - comp.lang.java.help Calling MATLAB ... BASIC problem calling LIB$ RTL - comp.os.vms... FROM$ AS TO$ Also declaring all variables and compiling ... the correct way to call OpenVMS functions from within ... LIB$ RTL - comp.os.vms calling a VB6 .DLL from java ... How to strip comments out of code - comp.lang.java.programmer ...Howdy...I need to write a class that will take a java ... result in a different file....Have your class call javac.-- ... (Keywords: Type erasure, compile-time constant expressions ... Question on GUI's? - comp.lang.java.gui/Thomas -- The comp.lang.java.gui FAQ: ftp://ftp.cs ... Matlab 7 uitable questions - comp.soft-sys.matlab Compiling ... Question | Groups | ... check the handles from within ... comp.lang.java.help - page 20Question about ant and running java programs 6 75 (4/15 ... started using Emacs and Ant to write my code and compile it. ... How does one find the terminal's width within the JVM? 6 94 ... Compiling, Executing, and Jar'ing Java Code - USFCSJumping into ~/USF/CS601/code/tools, I can compile T.java with the javac tool: ... Execute method main() within Jar'ing Java Code. Tool jar (java archive) is ... javac - Java programming language compilerThe arguments within a file can be space-separated or newline-separated. ... Here we use the Java 2 SDK javac to compile code that will run on a 1.1 VM. 7/27/2012 6:34:03 PM
|
|
|
|
|
|
|
|
|