简体   繁体   English

如何在运行时从jar文件加载实现接口的类?

[英]How to load a class which implements a interface from a jar file at runtime?

I want to implement an application which could use users implemented classes. 我想实现一个可以使用用户实现的类的应用程序。 Each class should be implemented an interface that I define the interfaces and at last, all of classes which implemented by a user, archived in a jar file. 每个类都应实现一个我定义接口的接口,最后,由用户实现的所有类都应存储在jar文件中。 Users copy the jar files in to class path and then only give the jar file name to my application. 用户将jar文件复制到类路径中,然后仅将jar文件名提供给我的应用程序。 My application should be able to load jar file dynamically. 我的应用程序应该能够动态加载jar文件。 To do this I found this post that is very useful, but it loads classes by class names. 为此,我发现这篇文章非常有用,但是它通过类名加载类。 I want to load classes based on their parents. 我想根据他们的父母来上课。

public class A implements iA {
    @Override
    int getAValue() { ... }
}

public class B implements iB {
     @Override
     int getBValue() { ... }
}

Suppose loaded jar file has both A and B classes. 假设加载的jar文件同时具有AB类。 I want to get an instance from class B by knowing the iB interface name. 我想通过了解iB接口名称从类B获取实例。

What should I do? 我该怎么办?

Check this post out: Find Java classes implementing an interface 看看这篇文章: 查找实现接口的Java类

It talks about the solution to what you want. 它讨论您想要的解决方案。

I know what I am going to suggest is not the lightest way to do it, but you just described one of the fundamental use cases for a plugin architecture in Java and the success story of OSGi. 我知道我要提出的建议并不是最简单的方法,但是您只是描述了Java插件体系结构的基本用例之一,以及OSGi的成功案例。

I would suggest using something light like Apache Felix, make the jar that the user provides implement a simple osgi.BundleActivator and register the implementation of your Interface as an OSGi service. 我建议您使用Apache Felix之类的工具,使用户提供的jar实现一个简单的osgi.BundleActivator,并将Interface的实现注册为OSGi服务。 (This is just a fancy hash map and nothing to do with REST services or the like). (这只是一个花哨的哈希图,与REST服务等无关)。

Then you can just obtain all the implementations of a given interface. 然后,您可以获取给定接口的所有实现。 Its pretty straight forward. 它非常简单。 You can check out the Felix documentation here: http://felix.apache.org/site/apache-felix-framework-launching-and-embedding.html 您可以在此处查看Felix文档: http : //felix.apache.org/site/apache-felix-framework-launching-and-embedding.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在运行时使用 Java 调试接口从 Jar 文件加载 class - How to load a class from a Jar file at runtime with the Java Debug Interface 如何从实现我的接口的JAR归档中加载类? (java) - How can I load a class from a JAR-archive which implements my interface? (java) 哪个类在运行时实现接口的方法 - Which class implements interface's method at runtime 如何通过接口从实现接口的类调用方法? - How to call a method from a class which implements an interface, through the interface? 如何在运行时从下载的jar文件中加载未知类? - How to load unknown class from downloaded jar file during runtime? Java:从实现接口的类访问接口函数 - Java: Accessing a interface function from a class which implements the interface 如何在运行时从jar从工作目录加载.class? - How to load .class from working directory at runtime from jar? 在jar中实现接口但不覆盖方法的类 - Class that implements an interface in a jar, but does not override the methods 具有来自继承的抽象类的值的构造函数,该类实现一个接口 - Constructor with values from inherited abstract class, which implements an interface 将接口传递给实现此接口的类 - pass interface to a class which implements this interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM