Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Search for a class in classpath

Posted on 2004-10-24
Medium Priority
Last Modified: 2013-11-23
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?

Question by:dtrusty
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 92

Assisted Solution

objects earned 750 total points
ID: 12397098
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.

Expert Comment

ID: 12397218
can u explain bit more???

Author Comment

ID: 12399908
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 92

Expert Comment

ID: 12405276
there is no easy way.

Accepted Solution

doronb earned 750 total points
ID: 12469820
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.

Expert Comment

ID: 12469996
Thanks :)
LVL 92

Expert Comment

ID: 12470481

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question