title: Loading classes runtime in Java
body: <p>I would like to make a service, which runs 24/7 and can load new modules or newer version of the already loaded modules without restarting it.</p>
  
  <p>I can load classes from jar files with something like this:</p>
  
  <pre><code>@FunctionalInterface
  public interface IWorker {
      void doStuff();
  }
  IWorker worker;
  try {
      URL[] urls = { new URL("jar:file:" +  "C:\\Users\\...\\out\\artifacts\\workers_jar\\somethingworker.jar"  +"!/") };
      ClassLoader cl = new URLClassLoader(urls);
      Class cls = cl.loadClass("hu.test.worker.SomethingWorker");
      worker = (IWorker) cls.newInstance();
      worker.doStuff();
  } catch (MalformedURLException e) {
      e.printStackTrace();
  } catch (ClassNotFoundException e) {
      e.printStackTrace();
  } catch (InstantiationException e) {
      e.printStackTrace();
  } catch (IllegalAccessException e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>The software already uses a database table for some configuration for the classes loaded from the modules, so If I store the full classname there (and maybe a path to the jars), I could use this. My question is, can I do this a better way? This is somewhat  hard to maintain and can break easily, also I don't know If I can load already loaded classes with it.</p>