samedi 21 octobre 2017

Getting a value from the class in a JAR file

I want to run a method in JAR, and the method will look like public static SomeType getValue().

I tried to use reflection and it seemed okay:

URLClassLoader classLoader = new URLClassLoader(new URL[]{jarFile.toURI().toURL()}, Main.class.getClassLoader());
Class targetClass = classLoader.loadClass("some.package.TargetClass");
Class targetType = classLoader.loadClass("some.package.SomeType");
Method getValueMethod = targetClass.getMethod("getValue");

But while getting a return value like this:

targetType value = targetClass.cast(getValueMethod.invoke(null));

value's type cannot be targetType, isn't it? However, the type SomeType is in the JAR file, so I can't do like SomeType value = ~~

Then, how can I get getValue()'s value properly and access its data?





Aucun commentaire:

Enregistrer un commentaire