Search for a class in classpath

I'd like to be able to search for classes in my classpath that implement a certain interface.  Anyone know how/if this is possible?

Thanks
dtrustyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
not easily, you'd need to go thru each entry in path and if a directory go thru all the class files in the directory, load them, and check if they implement inteface.
Similiarily for any jars in classpath, you'd need to open jar and check every file in jar.
0
nadhuviCommented:
can u explain bit more???
0
dtrustyAuthor Commented:
Well, I basically want to load a drop down full of classes that a user can use.  All of the classes need to implement a certain interface.  If the only way is to go through each entry in path, is there a way to get a list of all of the classes in a classpath?  Thanks  again
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

objectsCommented:
there is no easy way.
0
doronbCommented:
objects gave you the right way to go about doing this.  All I have to add to this is that looking through the classpath entries and locating *.class files is the easier part because there are actaully two ways to check if any given class implements any given interface.  The first and programatically easiest is to load the class and check if the class implements the interface.  The 2nd way (which you'd be forced to use IF, and only if you really don't want to load the class) is to actually read the constant-pool of the class (meaning load classbyes into memory, a byte[] array would do nicely for that) and check for yourself if the interface is referenced by the classbytes.

Code can be written for both approaches, but you'd have to tackle the class-discovery mechanism first anyway so to start you off on that here's some code:

  // Get the Java-Class-Path..
  String paths = System.getProperty("java.class.path");
  // Seperate paths..
  StringTokenizer st = new StringTokenizer(paths, File.pathSeparator);
  // Go through all entries..
  while (st.hasMoreElements()) {
    // Get the next classpath entry as a String..
    String ss = (String)st.nextElement();
    try {
      // Convert classpath entry to a file..
      File classPathEntry = new File(ss);

Now you have to check if the classpath entry is a director, or a JAR/ZIP file.  If its a directory, you have to look for all the *.class files in that directory, if it is a JAR/ZIP file, you have to load that file into memory using the JAR/ZIP api's and check each class inside it.

Good luck and I hope I've helped to clear things a bit.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
doronbCommented:
Thanks :)
0
objectsCommented:
(:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.