1 Reply Latest reply: May 4, 2010 6:01 AM by 843798 RSS

    Reload class

    843798
      I'm have problem with reload class in java. I'm read topic about "Dynamically Reloading a Modified Class" http://www.exampledepot.com/egs/java.lang/reloadclass.html and make exmaple, as that writen. But he not worked. What it is problem?
      public interface MyReloadableClass {
          public String myMethod();
      }
      public class MyReloadableClassImpl implements MyReloadableClass {
          public String myMethod() { 
              return "message";
          }
      }
      import java.io.File;
      import java.net.MalformedURLException;
      import java.net.URL;
      import java.net.URLClassLoader;
      
      public class Loader {
          private MyReloadableClass myObj;
      
          public MyReloadableClass getMyObj() {
              return myObj;
          }
      
          public Loader() {
              reloadMyReloadedClass();
          }
      
          private synchronized void reloadMyReloadedClass() {
              URL[] urls = null; try { // Convert the file object to a URL
                  File dir = new File("MyReloadableClassImpl.class");
                  URL url = dir.toURL(); // file:/c:/almanac1.4/examples/
                  urls = new URL[]{url};
              } catch (MalformedURLException e) {
                  e.printStackTrace();
              }
      
              try { // Create a new class loader with the directory
                  ClassLoader cl = new URLClassLoader(urls); // Load in the class
                  Class cls = cl.loadClass("MyReloadableClassImpl"); // Create a new instance of the new class
                  myObj = (MyReloadableClass) cls.newInstance();
              } catch (IllegalAccessException e) {
                  e.printStackTrace();
              } catch (InstantiationException e) {
                  e.printStackTrace();
              } catch (ClassNotFoundException e) {
                  e.printStackTrace();
              }
          }
      }
      public class RunLoad {
          public static void main(String[] arg) {
                 try {
                     while (true) {
                         Loader loader = new Loader();
                         System.out.println(loader.getMyObj().myMethod());
                         Thread.sleep(5000);
                     }
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
             }
      
      }
      After change class MyReloadableClassImpl and recompile he. no change.
      Have you any idea, what is it problem?
        • 1. Re: Reload class
          843798
          P1tBull wrote:
          I'm have problem with reload class in java. I'm read topic about "Dynamically Reloading a Modified Class" [http://www.exampledepot.com/egs/java.lang/reloadclass.html] and make example, as that writen. But he not worked. What it is problem?
          [http://www.exampledepot.com/egs/java.lang/reloadclass.html]
          It is important that the reloadable class not be on the classpath. Otherwise, the class will be loaded by some parent of the new class loader rather than by the new class loader itself. Once this happens, the class cannot be reloaded.