0

This question is so popular but I can't find the way to avoid the "java.lang.ClassNotFoundException".

I had implement a method to load dynamicly some external classes (that I got after extracting a war file)

  Class<?> classGetter() {
    URL url;
    Class<?>  cls = null;
    try {
        String className=new Substringer(entireFilePath.getName(), 5).output;
        String classPath=new 
        Substringer(entireFilePath.getAbsolutePath(),5+className.length()).output;
        File classDirPath = new File(classPath);
        url = classDirPath.toURI().toURL();
        URL[] urls = new URL[]{url};
        URLClassLoader cl = new URLClassLoader(urls); 
        cls = cl.loadClass(packageGetter()+"."+className);
        cl.close();
    } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
    }
    return cls;
}

the same exception still show up again and again. here is the stacktrace:

java.lang.ClassNotFoundException: bber.Counter
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at earviewer.ClassesLoaser.classGetter(ClassesLoaser.java:48)
    at earviewer.ClassesLoaser.main(ClassesLoaser.java:58)
    Exception in thread "main" java.lang.NullPointerException
    at earviewer.ClassesLoaser.main(ClassesLoaser.java:59)
Moh Lamine
  • 72
  • 1
  • 9

1 Answers1

0

This error occurs when you give fully qualified name of the class but try to load it inside the package. we should select the path to the class file without enterring to the package folder

Moh Lamine
  • 72
  • 1
  • 9