Here's my situation: I have a class in project A. I want to load this class in a method in project B without adding project A (or a JAR containing the class) to my project B classpath (I personally have no objection to doing it that way, but I must build this to spec).
Note that I will not know the absolute path to this class (let's call it "MyClass") as project A may be installed various different locations. I will, however, know how to navigate to it based on my current directory, so that is what I have done.
Here is what I have tried:
// Navigate to the directory that contains the class
File pwd = new File(System.getProperty("user.dir"));
File src = pwd.getAbsoluteFile().getParentFile();
File dir = new File(src.getAbsolutePath() + File.separator + "folder" + File.separator + "src");
// dir is the directory that contains all the packages of project A
// Load the class
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] {dir.toURI().toURL()});
try {
classLoader.loadClass("com.blahblahblah.MyClass");
} catch (ClassNotFoundException exception) {
}
This throws a ClassNotFoundException. Am I doing something wrong?