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

  • Home
  • SEARCH
  • 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 7699471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:29:10+00:00 2026-05-31T22:29:10+00:00

I have a String with some value. I want to iterate over all classes

  • 0

I have a String with some value. I want to iterate over all classes in a package calling a specific method, and if the value returned by the method is equals the value in my String, create an object of that class.

I want do this in a dynamic way, so if I add a class in this package, it will automatically be in the iteration.

Is possible do this? If not, there is a way to do what I want?


I was expecting something like.

for(Class clazz: SomeClass.listClasses("org.package")){
     //code here
}
  • 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-31T22:29:12+00:00Added an answer on May 31, 2026 at 10:29 pm

    No, this is not possible in a totally reliable way; however, it may be practical to implement in many cases.

    Due to the flexibility of Java class loaders, the classes defined under a particular package may not be known at runtime (e.g. consider a special classloader which defines classes on-the-fly, perhaps by loading them from the network, or compiling them ad-hoc). As such, there is no standard Java API which allows you to list the classes in a package.

    Practically speaking, however, you could do some tricks by searching the classpath for all class and JAR files, building a collection of fully-qualified class names, and searching them as desired. This strategy would work fine if you are sure that no active classloader will behave as described in the previous paragraph. For example (Java pseudocode):

    Map<String, Set<String>> classesInPackage = new HashMap();
    for (String entry : eachClasspathEntry()) {
      if (isClassFile(entry)) {
        String packageName = getPackageName(entry);
        if (!classesInPackage.containsKey(packageName)) {
          classesInPackage.put(packageName, new HashSet<String>());
        }
        classesInPackage.get(packageName).add(getClassName(entry));
      } else if (isJarOrZipFile(entry)) {
        // Do the same for each JAR/ZIP file entry...
      }
    }
    classesInPackage.get("com.foo.bar"); // => Set<String> of each class...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use an event like as i have some value in string
I have string value and Type of some variable (For example int and 32
HI, I have some string from XML file, and I I want to replace
I have a string such as Value = $val$ and I want to replace
Lets say I have some binary value: 0b100 and want to convert it to
I have a string that contains some text: <p>ProductImage [height:60]</p> and I want to
I have some string resources, e.g. a user welcome string. By default it should
I have some String[] arrays, for example: ['a1', 'a2'] ['b1', 'b2', 'b3', 'b4'] ['c1']
I have a string: [\n['-','some text what\rcontains\nnewlines'],\n\n trying to parse: Regex.Split(@[\n['-','some text what contains
In an ASP.NET 2.0 website, I have a string representing some well-formed XML. I

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.