Solved

Search for a class in classpath

Posted on 2004-10-24
205 Views
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?

Thanks
0
Question by:dtrusty
    7 Comments
     
    LVL 92

    Assisted Solution

    by:objects
    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
     
    LVL 1

    Expert Comment

    by:nadhuvi
    can u explain bit more???
    0
     

    Author Comment

    by:dtrusty
    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
     
    LVL 92

    Expert Comment

    by:objects
    there is no easy way.
    0
     
    LVL 9

    Accepted Solution

    by:
    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
     
    LVL 9

    Expert Comment

    by:doronb
    Thanks :)
    0
     
    LVL 92

    Expert Comment

    by:objects
    (:
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    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…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

    856 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now