Loading a class from a pathpath class using Java Reflection

I want to load a class from this not in the class path. Is there a way that I load a class along a file path without being in the classpath? eg

ClassLoader.load("c:\MyClass.class"); 
+4
source share
2 answers

An example from here :

 // Create a File object on the root of the directory containing the class file File file = new File("c:\\myclasses\\"); try { // Convert File to a URL URL url = file.toURL(); // file:/c:/myclasses/ URL[] urls = new URL[]{url}; // Create a new class loader with the directory ClassLoader cl = new URLClassLoader(urls); // Load in the class; MyClass.class should be located in // the directory file:/c:/myclasses/com/mycompany Class cls = cl.loadClass("com.mycompany.MyClass"); } catch (MalformedURLException e) { } catch (ClassNotFoundException e) { } 
+6
source

Load the contents of the class into an array of bytes and use ClassLoader.html # defineClass (java.lang.String, byte [], int, int) manually.

+2
source

All Articles