Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3287396
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:29:18+00:00 2026-05-17T20:29:18+00:00

I have a package which contains an interface and several classes,some classes in this

  • 0

I have a package which contains an interface and several classes,some classes in this package implement that interface ,In one class that does not implement that interface,I want to write a method that returns an object of all classes which implement that interface,I dont know the name of classes which implement that interface,how can I write this method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:29:19+00:00Added an answer on May 17, 2026 at 8:29 pm

    Generally such functionality is missing in java reflection API. but your can probably implement it yourself pretty easily.

    Shortly you can use systemp property java.class.path to get all classpath elements. Split it using property path.separator. Then iterate over the resulted array, read each jar file using JAR API, instantiate each Class and check if your interface isAssignableFrom(theClass).

    Here is the code snippet that looks for all BSF engines available in your classpath. I wrote it for my blog post I am working on this days. This code has limitation: it works with jar files only. I believe it is enough to explain the idea.

    private static Map<String, Boolean> getEngines() throws Exception {
        Map<String, Boolean> result = new HashMap<String, Boolean>();
        String[] pathElements = System.getProperty("java.class.path").split(System.getProperty("path.separator"));
        for (String pathElement : pathElements) {
            File resource = new File(pathElement);
            if (!resource.isFile()) {
                continue;
            }
            JarFile jar = new JarFile(resource);
            for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
                JarEntry entry = e.nextElement();
                if(entry.isDirectory()) {
                    continue;
                }
                if(!entry.getName().endsWith("Engine.class")) {
                    continue;
                }
                String className = entry.getName().replaceFirst("\\.class$", "").replace('/', '.');
                try {
                    if(BSFEngine.class.getName().equals(className)) {
                        continue;
                    }
                    Class<?> clazz = Class.forName(className);
                    if(BSFEngine.class.isAssignableFrom(clazz) && !clazz.equals(BSFEngine.class)) {
                        result.put(className, true);
                    }
                } catch (NoClassDefFoundError ex) {
                    // ignore...
                    result.put(className, false);
                }
            }
        }
        return result;
    }
    

    The method returns Map where engine class name is used as a key and boolean value indicates whether the engine is available. Engine is unavailable if it requires additional classes in classpath.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this .bat script which I use to maven package my application. Problem
I have written a python package which consists of several .py files which contain
I have created an installation package using Wix which installs a Windows service on
I have an SSIS package, which depending on a boolean variable, should either go
I have 2 different webapps (package into different war files) which needs to share
I have a package that I just made and I have an old-mode that
I have a dtsx package with a precedence constraint that evaluates an expression and
I have a DTS package that drops a table then creates it and populates
I have an SSIS package that connects to a mysql server and attempts to
Say I have a package mylibrary. I want to make mylibrary.config available for import,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.