Is it possible to somehow 'import' a new Java class into a running program and make use of it?
Could I have a program create a new File of type '.java' and then include it in the project files and reference it without having to restart the program?
The following is an example of what I mean:
import java.io.*;
public class Program {
File JClass = new File("JClass.java");
public static BufferedWriter out = null;
public static void main(String[] args) {
try {
out = new BufferedWriter(new FileWriter("JClass.java"));
out.write("public abstract class JClass {");
out.newLine();
out.newLine();
out.write(" public void printSomething(String a) {");
out.newLine();
out.write(" System.out.println(a);");
out.newLine();
out.write(" }");
out.newLine();
out.write("}");
out.close();
} catch (IOException e)
{
System.exit(-1);
}
//Somehow import JClass.java as a class here
JClass.printSomething("Yay! It worked!");
}
}
Resulting 'JClass.java' file:
public abstract class JClass {
public void printSomething(String a) {
System.out.println(a);
}
}
Similarly, would it be possible to create a copy of one of the project's source files, edit the code in the file, and then somehow force the changes upon the running program?
I do not care so much about practical application at this point. I am merely exploring different ideas I have had relating to programming. I also understand that this is potential for all sorts of disaster. Editing running code, and including classes on the fly (which I don't imagine would be checked for errors as the other classes are when the project is built), could have very unpredictable results. I merely want to play around with the idea.
That said, if anyone has any HELPFUL warnings or things to look out for, I would appreciate them. Otherwise, I would appreciate if people kept from responding 'This is a bad idea' or 'there are easier and better ways to solve problems'. I am NOT trying to solve a problem with this. I am only exploring the idea.
So, is this possible?