How to execute a method remotely on a jar file
I have an application whose purpose is to call the same particular method in the same class in lots of different jar files spread across a file system and receive back a result. So I can be guaranteed that a certain c开发者_JAVA技巧lass and method will exist in a jar file.
I know where each jar file is located. Its quite easy for me to instantiate the class via reflection and call the method in that manner.
My problem is that I have many of these jar files. And if I use the above approach to call into each jar, I'm concerned that the memory consumption of my application will increase dramatically as I will have to load the entire jar file for each call.
Is there any way I can call these jars files other than reflection? System calls wont work as I need to receive back a value.
much appreciated...
You might run into problems with perm space, but this can be increased with -XX:MaxPermSize=
parameter.
Make sure you use a separate URLClassLoader for each JAR file and don't hold onto them so they can get GCd. Load the JAR file through the new URLClassLoader, then use Class.forName() to load.
Good luck!
I would do something like this pseudocode:
foreach jf : myJarFiles
jcl = new JarClassLoader(jf);
Class.forName(myClassName);
m = MyClass.getMethod(...);
m.invoke(...);
精彩评论