title: How to load Jar class dynamically in Wildfly 15
body: <p>*Getting following exception when trying to load an external Sample .jar at runtime using :
  Class clazz = Class.forName(jarClass);.</p>
  <p>Exception:
  Module &quot;deployment .test.war&quot; from Service Module Loader]
  java.lang.ClassNotFoundException: org.abc.xyz.def.abc.Sample from [Module &quot;deployment.test.war&quot; from Service Module Loader]</p>
  <p>*Have then modified the class loader from &quot;this.getClass().getClassLoader()&quot; to &quot;ClassLoader.getSystemClassLoader()&quot; but then it started throwing following exception  :</p>
  <p>deployment.test.war//org.abc.pqr.tasks.JavaDataPlayTask$1.run(JavaDataPlayTask.java:450) - Execution of Java Data Play package failed: org/abc/pqr/tasks/DataPlay
  java.lang.NoClassDefFoundError: org/abc/pqr/tasks/DataPlay</p>
  <p>*The piece of code that performs dynamic class loading is inside a Main.jar
  *Class inside the Sample.jar implements an interface &quot;DataPlay&quot; that is available in Main.jar</p>
  <p>Have tried approaches defined in the following link but none of them worked for me:</p>
  <p><a href="https://access.redhat.com/documentation/en-us/jboss_enterprise_application_platform/6.2/html/development_guide/sect-use_the_class_loader_programmatically_in_a_deployment" rel="nofollow noreferrer">https://access.redhat.com/documentation/en-us/jboss_enterprise_application_platform/6.2/html/development_guide/sect-use_the_class_loader_programmatically_in_a_deployment</a></p>
  <p><a href="https://kostenko.org/blog/2019/06/runtime-class-loading.html" rel="nofollow noreferrer">https://kostenko.org/blog/2019/06/runtime-class-loading.html</a></p>
  <p><a href="https://www.programmersought.com/article/99402552966/" rel="nofollow noreferrer">https://www.programmersought.com/article/99402552966/</a></p>
  <p><a href="https://coderanch.com/t/614007/java/Dynamic-loading-class-external-jar" rel="nofollow noreferrer">https://coderanch.com/t/614007/java/Dynamic-loading-class-external-jar</a></p>
  <p><a href="https://www.programmersought.com/article/37026364495/" rel="nofollow noreferrer">https://www.programmersought.com/article/37026364495/</a></p>
  <p><a href="https://stackoverflow.com/questions/6909306/jar-hell-how-to-use-a-classloader-to-replace-one-jar-library-version-with-anoth">Jar hell: how to use a classloader to replace one jar library version with another at runtime</a></p>
  <p><a href="https://stackoverflow.com/questions/26112874/how-to-load-classes-from-jar-file-at-runtime">How to load classes from jar file at runtime?</a></p>
  <p><a href="https://stackoverflow.com/questions/27187566/load-jar-dynamically-at-runtime">Load jar dynamically at runtime?</a></p>
  <p>*Is the approach mentioned in the following link is suitable?
  <a href="https://access.redhat.com/documentation/en-us/jboss_enterprise_application_platform/6/html/migration_guide/find_the_jboss_module_dependency1" rel="nofollow noreferrer">https://access.redhat.com/documentation/en-us/jboss_enterprise_application_platform/6/html/migration_guide/find_the_jboss_module_dependency1</a></p>
  <p>*Is Java Platform Module System, the only solution to this problem?</p>
  <pre><code>public class ClassPathHacker {
  private static Logger log = Logger.getLogger(ClassPathHacker.class);
  
  private static final Class[] parameters = new Class[] { URL.class };
  
  public static void addFile(String s) throws IOException {
      File f = new File(s);
      addFile(f);
  }// end method
  
  public static void addFile(File f) throws IOException {
      addURL(f.toURL());
  }// end method
  
  public static void addURL(URL u) throws IOException {
  
      log.debug(&quot;Loading jar file: &quot;+u);
      
      try {
          doClasssLoading(ClassLoader.getSystemClassLoader(), u);
      } catch (Throwable t) {
          log.debug(&quot;Error, could not add URL to system classloader&quot;);
          throw new IOException(
                  &quot;Error, could not add URL to system classloader&quot;);
      }// end try catch
  
  }// end method
  
  public static void addJar(String jarPath, ClassLoader classLoader) throws IOException {
      File f = new File(jarPath);
      URL u = f.toURL();
      log.debug(&quot;Class loader class: &quot;+classLoader.getClass());
      log.debug(&quot;Loading jar file: &quot;+u);
      
      log.debug(&quot;System class loader class: &quot;+ClassLoader.getSystemClassLoader().getClass());
  
      try {
          doClasssLoading(classLoader, u);
      } catch (Throwable t) {
          // t.printStackTrace();
          log.debug(&quot;Error, could not add URL to system classloader&quot;);
          throw new IOException(
                  &quot;Error, could not add URL to system classloader&quot;);
      }// end try catch
  }
  
  private static void doClasssLoading(ClassLoader classLoader, URL url) {
      if (classLoader instanceof URLClassLoader) {
          log.info(&quot;Type of URL ClassLoader&quot;);
          URLClassLoader sysloader = (URLClassLoader)classLoader;
          Class sysclass = URLClassLoader.class;
          try {
              Method method = sysclass.getDeclaredMethod(&quot;addURL&quot;, new Class[] { URL.class });
              method.setAccessible(true);
              method.invoke(sysloader, new Object[] { url });
          } catch (Exception var5) {
              var5.printStackTrace();
              throw new IllegalStateException(var5.getMessage(), var5);
          } 
      } else {
          log.info(&quot;Not of type of URL ClassLoader&quot;);
          try {
              Field field = classLoader.getClass().getDeclaredField(&quot;ucp&quot;);
              field.setAccessible(true);
              Object ucp = field.get(classLoader);
          
              Method method = ucp.getClass().getDeclaredMethod(&quot;addURL&quot;, new Class[] { URL.class });
              method.setAccessible(true);
              method.invoke(ucp, new Object[] { url });
          } catch (Exception exception) {
              exception.printStackTrace();
              throw new IllegalStateException(exception.getMessage(), exception);
          } 
      } 
  }
    
  public static String getMainClass(String jarPath) throws IOException {
      JarFile jarFile = new JarFile(new File(jarPath));
      Attributes attributes = jarFile.getManifest().getMainAttributes();
      String className = attributes.getValue(&quot;Main-Class&quot;);
      return className;
  }
  
  }
  </code></pre>