I have a based class within my Java application. I would like to add new classes on the fly based on my based class. Is there any way to do it while running the application.
If I understand the question correct, one way to do this can be by extending the BaseClass into an abstract class and dynamically creating the abstract class' implementation / instance.
eg :
public abstract class ExtendedAbstract extends BaseClass {
public abstract void abstractMethod();
}
And...
//create dynamically anywhere in your code.
ExtendedAbstract dynClass = new ExtendedAbstract(){
public void abstractMethod(){
//implementation
...
...
}
};
.. well if this is what you want.
07-27-2005, 01:49 AM
sjalle
If this could be done at all, then how would the already running code be able to use the new classes and invoke their methods ?
07-27-2005, 08:18 AM
ewoo
I am using the base class to call the corresponding method or use the reflective tool to do it.
Ideally, I would like to be able to construct a class into a string and have some kind of Java object that will compile the class into byte stream. Once the class is in ByteCode I can then use classloader to load it in and run it.
07-27-2005, 08:56 PM
Zodoz
AhHaHaHa!!! I found out how to do it. This question was so inspirational that I had to find a solution, and here it is:
Code:
//file: RealTimeClassCreation.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLClassLoader;
/*
* ClassCreationMain.java
*
* Created on July 27, 2005, 6:49 PM
*/
/**
* @author Zodoz
*/
public class ClassCreationMain {
BufferedWriter out;
public ClassCreationMain() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What do you want to say?");
String sayWhat = in.readLine();
//create the source file
File f = new File ("SayClass.java");
out = new BufferedWriter(new FileWriter(f));
write("public class SayClass extends TestSay {");
write("\tpublic String getText() {");
write("\t\treturn \""+sayWhat+"\";");
write("\t}");
write("}");
out.close();
/*****************************
*java source file is created
*****************************/
File f2 = new File ("");
URL url = new URL("file:"+f2.getAbsolutePath()+"\\");
URLClassLoader loader = new URLClassLoader(new URL[] { url }); //url is location
//that the classes can be found
Process p = Runtime.getRuntime().exec("javac -classpath build/classes SayClass.java"); //make sure your system path is setup to run javac
/*******************************************
* use if you want to see any errors:
*
* BufferedReader pIn = new BufferedReader(new InputStreamReader(p.getErrorStream()));
* String line = "";
* while((line = pIn.readLine())!=null)
* System.out.println(line);
***********************************/
p.waitFor(); //wait to finish the process, this line is a must
Class c = loader.loadClass("SayClass"); //load the class
Object o = c.newInstance(); //create an instance of that class
TestSay ts = (TestSay)o; //typecast it to the real class that it is
System.out.println(ts.getText()); //use it
}