Search code examples
javaclassloader

ClassNotFound exception when extracting a class from a jar


I've been unsuccessfully trying to get a class from a .jar file. The .jar is located at C:\CTF.jar and contains a .class file CaptureTheFlagRules in a folder named CTF. The following code does not work:

try {
    File jarFile = new File("C:\\CTF.jar");
    URLClassLoader urlClassLoader = URLClassLoader.newInstance(new URL[] 
        { jarFile.toURI().toURL() }, getClass.getClassLoader());
    Class<?> unknownClass = Class.forName("CaptureTheFlagRules", 
        true, urlClassLoader); 
    ....
} catch (MalformedURLException e) {
} catch (ClassNotFoundException e) {
}  

The code throughs a ClassNotFoundException at the forName() call. What do I need to do to get my class?


Solution

  • Try:

    Class<?> unknownClass = Class.forName("CTF.CaptureTheFlagRules", 
        true, urlClassLoader); 
    

    If it's in the CTF folder, it's probably in the CTF package.