Hi guys, I'm trying to dynamically load a class. But I'm having trouble with the jar file. This is a snippet of my my code:
That doesn't work. But if I replace the path to where the original file is at, it works fine.Code:File jarFile = null; URL url = null; try { String path = "C:\\Users\\Orange\\Documents\\Lab5\\RpcServer\\MaxMin.jar"; jarFile = new File(path); url = new URL("file", null, -1, jarFile.getAbsolutePath()); URLClassLoader classLoader = new URLClassLoader(new URL[] {url}); return classLoader.loadClass("nhu.lab5.maxmin.MaxMinImpl").newInstance();
I'm sure I copy the exact same file, but I don't understand why the first code would work but the second one doesn't. Help?Code:File jarFile = null; URL url = null; try { String path = "C:\\Users\\Orange\\Documents\\Lab5\\MaxMin\\dist\\MaxMin.jar"; jarFile = new File(path); url = new URL("file", null, -1, jarFile.getAbsolutePath()); URLClassLoader classLoader = new URLClassLoader(new URL[] {url}); return classLoader.loadClass("nhu.lab5.maxmin.MaxMinImpl").newInstance();
Thank you in advance.
It looks like your jar files is part of a package, which means the .class files MUST be in the correct folder.
You're awesome. Thank you!![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks