I have a jar file named ThreadPool.jar that is bundled with packages and packages contain set of classes.There is only one class in the whole ThreadPool.jar that extends an abstract class named Pool.I need a java program which can search that single class that extends Pool abstract class. I need the output in form of fully qualified name of class i.e. packagename.classname, e.g if Foo is a class that extends Pool and Foo resides in the package named static then the output should be "static.Foo".
Asked
Active
Viewed 635 times
-1
-
1Have you tried anything? This question currently reads as "give me code". This isn't a good fit for this site. – Boris the Spider Jun 14 '14 at 20:57
-
i have tried....I have loaded their services in form of jar files in the class path..but the trouble is to find his/her class that implements Pool..one option is that i ask the user by an input dialog to also supply the fully qualified class name and then i save the name in a plain text file but i want a user friendly application and i want my application to automatically search that class. I dont want to use Service providers i am using URLClassLoader. – user3741146 Jun 15 '14 at 03:21
-
See my answer: http://stackoverflow.com/questions/16546418/find-classes-implementing-an-interface-in-jar/34582640#34582640 – ipolevoy Jan 03 '16 at 23:05
1 Answers
0
A first option - Java SPI - if you want to patch the ThreadPool.jar - is to use the Java SPI (Service Provider Interface).
This provides the discovery of classes that implement some interface.
You make a text file /META-INF/services/PATHNAME-OF-INTERFACE
in which to specify one or more implementing classes.
A second option would be treat the jar as zip archive, walk through it, maybe not load every class with java means (terrible), but use some byte code library. Still horrible.

Joop Eggen
- 107,315
- 7
- 83
- 138
-
ok..can u further guide me about this...i have following scenario.. my users provide their services in form of jar files...now in your case should i have mutiple META-INF/Services/Pathname-of-interfaces for each jar or a single meta-inf/services/pathname-of interfaces that contains list of service providers for all the jars? i am unaware of this.. – user3741146 Jun 14 '14 at 21:53
-
The Client123.jar that provides an implementation of interface (client made) say a.b.Intf should have a directory /META-INF/services with a.b.Intf with the implementation class listed. Java delivers with SPI (ServiceLoader) to retrieve from an interface all implementating classes from all jars on the class path. The interface you provide, in a separate jar. This way works the XML parser (implementors Xerces, Xalan). – Joop Eggen Jun 15 '14 at 10:27