如何在 jar 文件上远程执行方法
我有一个应用程序,其目的是在分布在文件系统中的许多不同 jar 文件中的同一类中调用相同的特定方法并接收结果。所以我可以保证某个类和方法会存在于jar文件中。
我知道每个 jar 文件所在的位置。对我来说,通过反射实例化该类并以这种方式调用该方法非常容易。
我的问题是我有很多这样的 jar 文件。如果我使用上述方法调用每个 jar,我担心应用程序的内存消耗将急剧增加,因为我必须为每次调用加载整个 jar 文件。
除了反射之外,还有什么方法可以调用这些 jar 文件吗?系统调用不起作用,因为我需要接收一个值。
非常感谢...
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 class 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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会遇到永久空间问题,但这可以通过
-XX:MaxPermSize=
来增加范围。
确保为每个 JAR 文件使用单独的 URLClassLoader,并且不要保留它们,以免它们获得 GCd。
通过新的URLClassLoader加载JAR文件,然后使用Class.forName()加载。
祝你好运!
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: