ClassNotFoundException while loading a class from file with URLClassLoader

I am using the following code to create an instance of OdbcIniTarget class which implements the Target interface. The OdbcIniTarget class already exists in the application but is located in a different package and now should be loaded from the target directory which is not in the class path.

... // directory below is resolved to /D:/Workspace/<myproject>/targets/ private File targetsLocation = new File("targets"); ... private void loadTargets() { URL url = null; try { url = targetsLocation.toURI().toURL(); } catch (MalformedURLException e) { } URL[] urls = new URL[]{ url }; URLClassLoader classLoader = new URLClassLoader(urls); try { Target target = (Target)classLoader. loadClass("ch.blah.targets.nope.OdbcIniTarget"). newInstance(); // <--- this fails hard } catch (...) { ... } try { classLoader.close(); } catch (IOException e) { } }

I am getting the following exception.

java.lang.ClassNotFoundException: ch.blah.targets.nope.OdbcIniTarget at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at ch.blah.targets.TargetFactory.loadTargets(TargetFactory.java:74) at ch.blah.targets.TargetFactory.<init>(TargetFactory.java:41) at ch.blah.SomeOfMyClasses.<init>(SomeOfMyClasses.java:34) at ch.blah.TheMainClass.main(TheMainClass.java:38)

Does someone have an idea why the class is not found?

The class which is loaded looks as follows.

package ch.blah.targets.nope; import org.apache.log4j.Logger; import ch.blah.utils.Utils; public class OdbcIniTarget extends Target { private static final Logger log = Logger.getLogger(OdbcIniTarget.class); @Override public boolean someMethod1() { return false; } @Override public boolean someMethod2() { return false; } @Override public boolean someMethod3() { return false; } @Override public void someMethod4() { log.debug("Blah."); Utils.getInstance().marshalClass(SomeClass.class); log.debug("Finished."); } }

The directory structure for targets directory is as follows.

D:\Workspace\<myproject>\targets>dir Volume in drive D is DATA Volume Serial Number is 021C-EC9B Directory of D:\Workspace\<myproject>\targets 10.07.2014 21:20 <DIR> . 10.07.2014 21:20 <DIR> .. 10.07.2014 21:31 1'278 OdbcIniTarget.java 10.07.2014 20:23 3'761 OdbcIniTargetConfiguration.java 2 File(s) 5'039 bytes 2 Dir(s) 7'328'571'392 bytes free



It is failing to load because it doesn't contain classes at that URL, it has java source


Compile those java classes to .class file and then pass the url to the directory which should look like



try passing the caller's classloader as a parameter in the constructor:

URLClassLoader classLoader = new URLClassLoader(urls, getClass().class.getClassLoader());

the class is a direct child of the dir in the url, so you need no package:

Target target = (Target)classLoader. loadClass("OdbcIniTarget"). newInstance();


  • Class to store multiple comparators
  • Border width to change as per the inner div content width
  • ClassNotFoundException while loading a class from file with URLClassLoader
  • Read .jar file in Java applet
  • Why does my .jar file raise exceptions even if my program runs without exceptions in Eclipse?
  • Sample code to fix this particular SQL-injection hole
  • java.lang.ClassFormatError
  • Java Xpath query
  • java.lang.IncompatibleClassChangeError
  • Parse XML string and build a list of strings
  • Android java.lang.IllegalStateException in onRequestPermissionsResult()
  • Runtime error in UVA Online Judge [closed]
  • Symfony2 redirect to https route fails (uses wrong port)
  • creating instance of object using reflection , when the constructor takes an array of strings as arg
  • 3.0.0.M1: SSL - Invalid keystore format
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • How to access meteor package name inside package?
  • How to copy styled text in JTextPane
  • How to get latest version of a artifact on Bintray using JSONP
  • Tell Git to stop prompting me for conflicts when none really exist?
  • npm 5.4.1 install/uninstall all failing
  • Problem deserializing objects from cache on MyBatis 3/Java
  • How to test if a URL from an Eclipse bundle is a directory?
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Display issues when we change from one jquery mobile page to another in firefox
  • Optimizing database types to compact database (SQLite)
  • TFS: Get latest causes slow project reloading
  • Cross-Platform Protobuf Serialization
  • javascript inside java/jsp code
  • Running a C# exe file
  • Python - Map / Reduce - How do I read JSON specific field in using DISCO count words example
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to format a variable of double type
  • Android Studio and gradle
  • coudnt use logback because of log4j
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • JaxB to read class hierarchy
  • Reading document lines to the user (python)
  • How can i traverse a binary tree from right to left in java?