lundi 18 mai 2015

java.lang.NoClassDefFoundError on classloader from external jar on a repo

I am trying to load the external jar and create a structure of all the classes from that jar.

Currently, the jar is using a dependency on a different location on the repo.

Accessed jar is located at someurl/com/xyz/mystuff2.jar Accessed jar mystuff1.jar is dependent on someurl/com/abc/mystuff1.jar.

I tried the following.

       try {
      u = new URL("jar", "", this.url /* Some url*/+ "!/");

      URL[] urls = { u };             
      URLClassLoader cl = URLClassLoader.newInstance(urls);

      Enumeration<JarEntry> entries = this.jarFile.entries();

      while (entries.hasMoreElements()) {
          JarEntry entry = entries.nextElement();

          /* Soft checks to make sure it is not pulling an inner class
           */

          if (entry.isDirectory() || !entry.getName().endsWith(".class")
                  && !entry.getName().contains("$")) {
              continue;
          }

          String className = entry.getName().substring(0,
                  entry.getName().length() - 6);
          className = className.replace('/', '.');
          try {
              @SuppressWarnings("unused")
              Class<?> c = cl.loadClass(className); /*It errors while   
             loading a class that are extending classes from mystuff.jar*/

          } catch (ClassNotFoundException e) {
              logger.error("ERROR: Classes were not loaded properly "
                      + e.getMessage());

          }catch (NoClassDefFoundError e) {
                  logger.error("ERROR: Classes definition was not found "
                          + e.getMessage());
          }           }

  } catch (MalformedURLException e) {             
  logger.error("ERROR : There is an issue while accessing the 
  URL"+ e.getMessage());      

}

I am certainly missing a point here, since the jar is already compile and deployed, it should have the bindings from mystuff1 already. Also, given that the extended classes are abstract, what could be an alternative to get the classes loaded or know about them.

My goal is to able to retrieve all the classes, those that extends my BaseClass.class, through reflection and perform create some meaningful structure.

Thanks in advance!!





Aucun commentaire:

Enregistrer un commentaire