Reputation: 13
I am trying to load a class from a URL using java reflection and classLoader. The application is deployed on weblogic 12c server
File f = new File("/a/abc");
URL url = f.toURI().toURL();
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(classLoader, url);
Class jobClass = classLoader.loadClass("com.test.abc");
However, I get the below error:
exception thrown:
java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
Please let me know what might be the issue here or if this is not the correct way to do it.
Upvotes: 0
Views: 4813
Reputation: 81
You are invoking the method 'addURL' on a class of type ClassLoader. The class ClassLoader does not contain the member method 'addURL'. That's why you get the error.
Instead you should call the Method's 'invoke' method with an object of type URLClassLoader.
Upvotes: 1