Search code examples
javaclasspackageclassloaderclassnotfoundexception

Issue loading a class file from another package


I have an issue with loading classes via Class.forName(String className) when I amtrying to get a class from another package. It keeps throwing a ClassNotFoundException, even though I am fully declaring qualified (package) names as the class name.

Is there any way to load classes from another package?

Basically, what I am trying to do is load a bunch of different class files using a configuration. This isn't explicitly necessary (I'm just fooling around with code) but I would like to know if this is possible or not.

As a last note, I am fairly new to code development, having only just begun back in November, with Java as my first language.


Solution

  • It is totally possible. Two things to check:

    1. Use dots as separators: "java.lang.String", not "java/lang/String" or anything else
    2. Make sure the other package is loadable -- i.e., it has to be on the JVM's class path.