Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a working test. Every 5 secs Test.main() reloads test.Test1.class from the file system and calls Test1.hello()</p> <pre><code>package test; public class Test1 { public void hello() { System.out.println("Hello !"); } } public class Test { static class TestClassLoader extends ClassLoader { @Override public Class&lt;?&gt; loadClass(String name) throws ClassNotFoundException { if (name.equals("test.Test1")) { try { InputStream is = Test.class.getClassLoader().getResourceAsStream("test/Test1.class"); byte[] buf = new byte[10000]; int len = is.read(buf); return defineClass(name, buf, 0, len); } catch (IOException e) { throw new ClassNotFoundException("", e); } } return getParent().loadClass(name); } } public static void main(String[] args) throws Exception { for (;;) { Class cls = new TestClassLoader().loadClass("test.Test1"); Object obj = cls.newInstance(); cls.getMethod("hello").invoke(obj); Thread.sleep(5000); } } } </code></pre> <p>Run it. Then change and recompile Test1 </p> <pre><code>System.out.println("Hello !!!"); </code></pre> <p>while Test is running. You will see Test1.hello output changed</p> <pre><code>... Hello ! Hello ! Hello !!! Hello !!! </code></pre> <p>This is how eg Tomcat reloads webapps. It has a separate ClassLoader for each webapp and loads a new version in a new ClassLoader. The old one is GCed just like any Java object as well as the old classes.</p> <p>Note that we loaded Test1 with TestClassLoader and invoked its first method with reflection. But all Test1 dependencies will be implicitly loaded with Test1 class loader, that is all the Test1 application will be loaded by JVM into TestClassLoader. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload