I want to extract static field from another program. That program is launched through java reflection and URLClassLoader:
private static final String ProgramClassName = "com.myProject.MyMainClass";
private static final String ProgramLocation = "/home/user/program/22.3.0/launcher/target/program.jar";
private static final String[] ProgramArguments ={""};
<...>
URL[] urls = new URL[]{ new URL("jar:file:" + ProgramLocation +"!/")};
URLClassLoader cl = URLClassLoader.newInstance(urls, getClass().getClassLoader());
Class<?> classToLoad = Class.forName (ProgramClassName , true, cl);
Method method = classToLoad.getDeclaredMethod ("main", String[].class);
Object instance = classToLoad.newInstance ();
Object result = method.invoke (instance, (Object)ProgramArguments);
Class com.myProject.MyMainClass uses a static field of another class AnotherMyClass placed in different JAR.
That JAR is loaded properly. I know it because program return correct result (that is based on AnotherMyClass). But when i try to access AnotherMyClass field i get exception:
Class<?> anotherClass = Class.forName (AnoterhClassName, true, cl);
java.lang.ClassNotFoundException: com.myProject.MyMainClass
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587)
at java.base/java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:872)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:467)
How could i load exactly that instance of class that was used by method.invoke (instance, (Object)ProgramArguments);
call? Or maybe there is another way to launch another program and extract static field as result?
Aucun commentaire:
Enregistrer un commentaire