HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Plumbing Java Classes"
Vol. 2, Issue 7, p. 40

	

Listing 1
 
// This method can be called to compile the .Java file with the given filename. 
The reference to method "getCompilerClassPath()" is a placeholder for whatever 
mechanism you would use to build the classpath that the compiler should use. 
For example, you might give a list of JAR files, ZIP files or local file system directories. 
public static void compile(String fileName) { 
    // Run the javac compiler inside the current address space. 
    String astrArgs[] = { 
            "-classpath", 
            getCompilerClassPath(), 
            "-nowarn", 
            fileName 
    }; 

    // The sun.tools.javac.Main constructor takes two args: 
	the error output stream to use (which may be a file and pipe or whatever) 
	and the string “javac”. 
    sun.tools.javac.Main compiler = 
        new sun.tools.javac.Main(System.err, "javac"); 
    boolean fOk = compiler.compile(astrArgs); 

    if (!fOk) 
        System.err.println("Compilation of file " + fileName + 
                "failed!"); 
} 

Listing 2
 
// This static method compresses multiple source files into a single JAR file. 
The manifest file is an optional parameter which is ignored if it is null. 
Public static jar(String jarFile, String[] aFileNames, String manifestFile) { 
    String[] astrArgs = null; 
    if(null != manifestFile) { 
        astrArgs = new String[3+aFileNames.length]; 
        astrArgs[0] = "cfm"; 
    } else { 
        astrArgs = new String[4+aFileNames.length]; 
        astrArgs[0] = "cf"; 
    } 

    args[1] = jarFile; 

    int i = 2; 
    if(null != manifestFile) 
        args[i++] = manifestFile; 

    for (int j=0; j<aFileNames.length; j++) 
        args[i+j] = aFileNames[j]; 

    sun.tools.jar.Main jartool = 
        new sun.tools.jar.Main(System.out, System.err, "jar"); 

    boolean ok = jartool.run(args); 
    if (!ok) 
        error("Jar tool invocation failed"); 
}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.