How to create a dynamic class “on the fly” :
com.sun.tools.javac.Main class has two methods which help us to invoke the compiler from a program. It will behave like javac command.
public static int compile(String []args);
public static int compile(String []args , PrintWriter out);
args-> command line arguments that would be normally be passed on the javac program.
out-> indicates where the compiler’s output is directed.
return (int) -> exit value
I would like to share a simple program which will create a java file on the fly (ie at Runtime) and it will be compiled using compile method provided by this class com.sun.tools.javac.Main.Once itsĀ been compiled,using reflection apis we will invoke the method in the class(which is created at Runtime)..
import java.io.FileWriter;
import java.lang.reflect.Method;
public class DynamicClassCreation
{
//ClassName
static final String className=“DynamicTestClass”;
static final String fileName=className+“.java”;
public static void main(String []a)
{
createClass();
}
public static void createClass()
{
String methodName=“execute”;
String parameterName=“strParam”;
try{
//Creates DynamicTestClass.java file
FileWriter fileWriter=new FileWriter(fileName,false);
fileWriter.write(“public class “+ className +” {\n”);
fileWriter.write(“public String “+methodName +“(String “+parameterName+“) {\n”);
fileWriter.write(“System.out.println(\” Testing\”);\n”);
fileWriter.write(“return “+parameterName +“+ \” is dumb\”;\n }\n}”);
fileWriter.flush();
fileWriter.close();
String[] source = { new String(fileName) };
//javac -> compile method in Main class ..presents in tools.jar
//Compile the DynamicTestClass.java ,created on the fly
com.sun.tools.javac.Main.compile(source);
// Creates an object of type Class which contains the information of
// the class String
Class classObj=Class.forName(className);
// getDeclaredMethod() returns the declared methods of the class.
Method method= classObj.getDeclaredMethod(methodName,new Class[] { String.class}); //for int -> Integer.TYPE;
//Invoking the execute method and passing the value for String parameter
String returnVal =(String) method.invoke(classObj.newInstance(),new Object[] { “Vignesh”});
System.out.println(“Value return from DynamicTestClass : “+ returnVal);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Before compiling this class, we should set the classpath for tools.jar file..
C:\java>set CLASSPATH=C:\Java\jdk1.5.0_16\lib\tools.jar;%CLASSPATH%
Compile : javac DynamicClassCreation.java
Run : java DynamicClassCreation
Note:
target option:
We have jdk 1.5, but we would like to compile the code that will run on a 1.4 VM.-target 1.4 option ensures that the generated class files will be compatible with 1.4 VMs.
EX:
javac -target 1.4 -bootclasspath jdk1.4.2/lib/classes.zip \
-extdirs “” TestClass.java
Reference:
Javac: http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/javac.html
Is this package “com.sun.tools.javac.main” part of JDK. ?
There is a similiar package that does this thing and its part of JBoss. The jar name is javassist.
http://www.csg.is.titech.ac.jp/~chiba/javassist/v1/html/index.html
Thanks for this..was looking for something like this.