Compiling with javac from within Java

Discussion in 'Java' started by Gordon Beaton, Nov 5, 2005.

  1. 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
    Gordon Beaton, Nov 5, 2005
    #1
    1. Advertising

  2. Gordon Beaton

    Roedy Green Guest

    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.
    Roedy Green, Nov 5, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tasos Kotsikonas

    Calling javac and jar from a Java class

    Tasos Kotsikonas, Nov 17, 2003, in forum: Java
    Replies:
    3
    Views:
    2,748
    Roedy Green
    Nov 17, 2003
  2. Jesper Sahner
    Replies:
    7
    Views:
    7,888
    Jesper Sahner
    Nov 21, 2004
  3. John Morey
    Replies:
    6
    Views:
    443
    Chris Uppal
    Nov 24, 2004
  4. Replies:
    1
    Views:
    509
    Andrew Thompson
    Oct 31, 2005
  5. Catherina Svensson
    Replies:
    0
    Views:
    275
    Catherina Svensson
    Apr 29, 2008
Loading...

Share This Page