Re: ClassLoader not loading recompiled classes



On Oct 2, 6:02 am, Daniel Pitts <googlegrou...@xxxxxxxxxxxxx> wrote:
On Oct 1, 10:53 pm, "Aryeh M. Friedman" <Aryeh.Fried...@xxxxxxxxx>
wrote:



ClassLoader does not update class on recompile:

Script started on Tue Oct 2 01:45:20 200> cat Main.java

public class Main
{
public static void main(String[] args)
throws Throwable
{
while(true) {
ClassLoader loader=ClassLoader.getSystemClassLoader();
Class klass=loader.loadClass("MyClass");
MyClass mc=(MyClass) klass.newInstance();

System.out.println("hit any key to reload/rerun MyClass");
System.in.read();
}
}}
cat MyClass.java

public class MyClass
{
public MyClass()
{
System.out.println("not hi there");
}}
javac *.java
java Main

not hi there
hit any key to reload/rerun MyClass

not hi there
hit any key to reload/rerun MyClass
^Z
Suspended> cat foo

public class MyClass
{
public MyClass()
{
System.out.println("foo on you");
}}
mv foo MyClass.java
javac MyClass.java
fg

java Main

not hi there
hit any key to reload/rerun MyClass
^C> exit

Script done on Tue Oct 2 01:47:02 200

Right, a ClassLoader will not re-load a class. You will have to
instantiate a new class loader to do so.
ClassLoader.loadClass will first look for already loaded classes. It
will not re-load the class into the JVM.

Its generally difficult to get dynamic class behavior from Java. You
aren't able to unload a class, and load a different version of it.
Also, and already loaded classes that refer to that other class will
only be able to refer to one instance of it, not one from one class
loader, and then another from another class loader.

Since I am relativally naive with class loaders how do I create a new
instance of the system class loader?

.



Relevant Pages

  • Re: empty interfaces via reflection
    ... Have a custom class loader since the system ... MyClass ... public class MyClassLoader extends ClassLoader ...
    (comp.lang.java.programmer)
  • Re: ClassLoader not loading recompiled classes
    ... MyClass mc=klass.newInstance; ... public class MyClass ... java Main ... and then another from another class loader. ...
    (comp.lang.java.programmer)
  • Re: ClassLoader not loading recompiled classes
    ... MyClass mc=klass.newInstance; ... public class MyClass ... java Main ... and then another from another class loader. ...
    (comp.lang.java.programmer)
  • ClassLoader not loading recompiled classes
    ... ClassLoader does not update class on recompile: ... MyClass mc=klass.newInstance; ... public class MyClass ... hit any key to reload/rerun MyClass ...
    (comp.lang.java.programmer)
  • Re: empty interfaces via reflection
    ... public class MyClassLoader extends ClassLoader ... to load in MyClass itself, ... not in the program's classpath, the system classloader won't load it; ...
    (comp.lang.java.programmer)