Java How to load classes from jar in classpath using Class ClassLoader (no URLClassLoader)?

So, I need to load some class at runtime using System ClassLoader from jar in the classpath, but every time I try, I get a ClassNotFoundException. With System ClassLoader, I can only do: xyclassineed (x and y are packages), or will I need to do something like: pathtox.xyclassineed, assuming this is possible?

0
source share
1 answer

JAR should not be in your CLASSPATH.

This works fine: I have a JDOM JAR in my CLASSPATH.

package cruft;

/**
 * ClassLoaderDemo
 * @author Michael
 * @since 2/9/12 7:09 PM
 * @link http://stackoverflow.com/questions/9220887/java-how-to-load-classes-out-of-a-jar-in-the-classpath-with-the-system-classload
 */
public class ClassLoaderDemo {
    public static void main(String[] args) {
        try {
            ClassLoader classLoader = ClassLoaderDemo.class.getClassLoader();
            if (classLoader != null) {
                Class clazz = classLoader.loadClass("org.jdom.Document");
                System.out.println(clazz.getName());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
+1
source

All Articles