7 Sept 2012

How can I load a *.java class file into my java application


You can do it by using classes inside javax.tools. 
You will have a ToolProvider class from which you can obtain a compiler instance and compile code at runtime. 
Later you will load .class files just compiled separately with a ClassLoader unless you obtain directly 
a binary code for the class and you are able to istantiate it directly.



public class SimpleTest
{
  public static void main(String[] args) throws Throwable
  {
    String filename = args[0];
    String className = args[1];
    SimpleCompiler compiler = new SimpleCompiler(filename);
    ClassLoader loader = compiler.getClassLoader();
    Class compClass = loader.loadClass(className);
    Object instance = compClass.newInstance();
  }
}

0 comments:

Post a Comment