Web services test code generator
Klaus Berg has recently released a test-code generator for JUnit-based Web service clients. If you're developing Web services using Axis2 and XMLBeans this wizard could turn your JUnit test client coding into a powerful code generation process. It also has uses for those using GUI-based testing tools like soapUI.

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Java Tip 131: Make a statement with javac!

Interactively explore short bits of Java

Often you may want to test a single piece of code. For example, say you forget how the % operator works with negative numbers, or you must determine how a certain API call operates. Writing, compiling, and running a small program repeatedly just to test small things can prove annoying.

With that in mind, in this Java Tip, I present a short program that compiles and runs Java code statements simply by using tools included in Sun's JDK 1.2 and above.

Note: You can download this article's source code from Resources.

Use javac inside your program

You'll find the javac compiler in the tools.jar library found in the lib/ directory of your JDK 1.2 and higher installation.

Many developers do not realize that an application can access javac programmatically. A class called com.sun.tools.javac.Main acts as the main entry point. If you know how to use javac on the command line, you already know how to use this class: its compile() method takes the familiar command-line input arguments.

Compile a single statement

For javac to compile any statement, the statement must be contained within a complete class. Let's define a minimal class right now:

    /**
     * Source created on <this date>
     */
    public class <Temporary Class Name> {
        public static void main(String[] args) throws Exception {
            <Your Statement>
        }
    }


Can you figure out why the main() method must throw an exception?

Your statement obviously goes inside the main() method, as shown, but what should you write for the class name? The class name must possess the same name as the file in which it is contained because we declared it as public.

Prepare a file for compilation

Two facilities included in the java.io.File class since JDK 1.2 will help. The first facility, creating temporary files, frees us from choosing some temporary name for our source file and class. It also guarantees the file name's uniqueness. To perform this task, use the static createTempFile() method.

The second facility, automatically deleting a file when the VM exits, lets you avoid cluttering a directory or directories with temporary little test programs. You set a file for deletion by calling deleteOnExit().

Create the file

Choose the createTempFile() version with which you can specify the new file's location, instead of relying on some default temporary directory.

Finally, specify that the extension must be .java and that the file prefix should be jav (the prefix choice is arbitrary):

    File file = File.createTempFile("jav", ".java",
            new File(System.getProperty("user.dir")));
    // Set the file to delete on exit
    file.deleteOnExit();
    // Get the file name and extract a class name from it
    String filename = file.getName();
    String classname = filename.substring(0, filename.length()-5);


Note that you extract the class name by removing the .java suffix.

Output the source with your short code segment

Next, write the source code to the file through a PrintWriter for convenience:

    PrintWriter out = new PrintWriter(new FileOutputStream(file));
    out.println("/**");
    out.println(" * Source created on " + new Date());
    out.println(" */");
    out.println("public class " + classname + " {");
    out.println("    public static void main(String[] args) throws Exception {");
    // Your short code segment
    out.print("        "); out.println(text.getText());
    out.println("    }");
    out.println("}");
    // Flush and close the stream
    out.flush();
    out.close();


The generated source code will look nice for later examination, with the added benefit that if the VM exits abnormally without deleting the temporary file, the file will not be a mystery if you stumble upon it later.

The short code segment, if you notice, is written with text.getText(). As you will see shortly, the program uses a small GUI (graphical user interface), and all your code will be typed into a TextArea called text.

Use javac to compile

To use the compiler, create a Main object instance, as mentioned above. Let's use an instance field to hold this:

    private com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();


A call to compile() with some command-line arguments will compile the aforementioned file. It also returns a status code indicating either success or a problem with the compile:

    String[] args = new String[] {
        "-d", System.getProperty("user.dir"),
        filename
    };
    int status = javac.compile(args);


Run the freshly compiled program

Reflection nicely runs code inside an arbitrary class, so we'll use it to locate and execute the main() method where we placed our short code segment. In addition, we process the returned status code by displaying an appropriate message to give users a clean and informative experience. We found the meaning of each status code by decompiling javac, hence we have those weird "Compile status" messages.

The actual class file will reside in the user's current working directory, as already specified with the -d option to the javac instance.

A 0 status code indicates that the compile succeeded:

    switch (status) {
        case 0:  // OK
            // Make the class file temporary as well
            new File(file.getParent(), classname + ".class").deleteOnExit();
            try {
                // Try to access the class and run its main method
                Class clazz = Class.forName(classname);
                Method main = clazz.getMethod("main", new Class[] { String[].class });
                main.invoke(null, new Object[] { new String[0] });
            } catch (InvocationTargetException ex) {
                // Exception in the main method we just tried to run
                showMsg("Exception in main: " + ex.getTargetException());
                ex.getTargetException().printStackTrace();
            } catch (Exception ex) {
                showMsg(ex.toString());
            }
            break;
        case 1: showMsg("Compile status: ERROR"); break;
        case 2: showMsg("Compile status: CMDERR"); break;
        case 3: showMsg("Compile status: SYSERR"); break;
        case 4: showMsg("Compile status: ABNORMAL"); break;
        default:
            showMsg("Compile status: Unknown exit status");
    }


An InvocationTargetException throws when code executes through reflection and the code itself throws some exception. If that happens, the InvocationTargetException is caught and the underlying exception's stack trace prints to the console. All other important messages are sent to a showMsg() method that simply relays the text to System.err.

Resources