I am trying to edit a class file and make it available to the JVM at runtime. Is that possible, how can I do it?
例如:
main() {
for (int i=0;i<10;i++) {
NewClass.method();
//ask user if to continue..
}
}
public class NewClass() {
static void method() {
sysout("hi");
}
}
当此循环执行时, 我要更改文件 NewClasss () 并在 JVM 中装入它, 以便它打印“ 再见 ” 。
这是完整的代码:
try {
for (int iCount = 0; iCount < 10; iCount++) {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.print("Enter which method:");
int i = Integer.parseInt(br.readLine());
System.out.println(i);
if (i == 1) {
Called.calledMethod1();
} else {
Called.calledMethod2();
}
}
} catch (NumberFormatException nfe) {
System.err.println("Invalid Format!");
}
我想运行一个类的主要方法, 当它运行时, 我想引用到另一个类, 编辑它, 并且将第二个类 以相同的方法引用, 并获得不同的输出 。
我 don t 想要:
- stop the jvm
- edit the code
- run the code again.
我想,我想,我想
- edit the code at run time, and get the changes reflected immediately.