Wednesday, November 28, 2012

Create new Java Class files at runtime

When we write programs usual method is to write all the code compile it and run. But when we try to complex things some times we may want to generate a new class and use it at runtime. As an example I wanted to create a class which parse a xml message and create a new Java object from that data. This is used in web server. So that result object varies according to the web service which is deployed. The solution was to inspect the service at deployment time and create a new parser class according to that in the service deployment time. It has to be noted that this parser had to be generated as a .class file and dynamically added to the classpath. So the result was to use a bytecode manupulation library. There are few bytecode manuplulation libraries available as ASM and JavaAssist.
I used JavaAssist for this. The reason for using JavaAssist is it is simple to create a new class from the scratch using JavaAssit. When we create new class files using JavaAssit we have to create all the methods in the new class as Strings and the pass them to Java Assist. As a example if we have to add amethod called sayHellow() to new class we can create it as a String like this.
String s="public void sayHello(){ System.out.println("Hello"); }

     * @param c   Any class which can be used to get the class loader                          (c.getClassLoarder()) can be passed using object.getClass() methood
     * @param name
     *            Name of the to be generated class
     * @param methods
     *            Methods to be created as Strings
     * @param interfaces
     *            interfaces to be implemented as Strings
     * @param directory
     *            the directory generated class files has to be written
    public static void createClass(Class<?> c, String name, List<String> methods,
            List<String> interfaces, String directory) {
        String temp = null;
        try {
            ClassPool pool = ClassPool.getDefault();
            pool.insertClassPath(new ClassClassPath(c));
            CtClass cc = pool.makeClass(name);
            if (interfaces != null) {
                for (String s : interfaces) {
                    CtClass anInterface = pool.get(s);
            for (String s : methods) {
                temp = s;
                CtMethod m = CtNewMethod.make(s, cc);
        } catch (Exception e) {
            // TODO throw

To add this class to the classpath following method can be used.

//Here directory is the path which class files were written

 public static Class loadClass(String className,String directory, ClassLoader loader) throws Exception {
        File f = new File(directory);[] urls = new[] { f.toURI().toURL() };
        ClassLoader cl = new URLClassLoader(urls, loader);
        Class cls = cl.loadClass(className);
        return cls;

Note - JavaAssist does not support generics. If we use genarics java assist throws an exception. We have to use fully qualified names for types. As an example we have to use java.util.List for List type.


  1. You can create class which implements a specific interface dynamically and Load that class dynamically and assign to a interface reference of that particular type. Then you can call the methods which are in the interface. The method implementation can be changed when you are creating the class at runtime.

  2. "Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.

  3. This is very informative blog for learners, Thanks for sharing this information Android Online Training Hyderabad

  4. • Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating. Power Bi Online Training