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 7032813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:59:00+00:00 2026-05-28T00:59:00+00:00

Given a class name as a string, how do I get the package name

  • 0

Given a class name as a string, how do I get the package name of it at run time ? I do not have the fully qualified name with package name + class name. Simply only the class name.

I want the package name to be used in Class.forName() method.

I am perfectly fine with finding the first matching package name (if multiple packages have the same class).

Any ideas?

UPDATE

I DO NOT have a Class instance to work on. My requirement is to create a Class using the Class.forName() method. But I simply have ONLY the class name as a string. I need some way to loop though the packages and identify if the class I have belongs to the package.

The stack trace of the exception is

Exception in thread "main" java.lang.ClassNotFoundException: MyAddressBookPage
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
  • 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-28T00:59:00+00:00Added an answer on May 28, 2026 at 12:59 am

    This is most likely an incredibly inefficient, bloated, inconvenient way of doing what you’re trying to achieve, and hopefully there’s already an out-of-the-box, single way to do it… but it should work.

    Basically, scan through every class in the class path, until you find a class where getSimpleName() matches the class name you have.

    I recommend looking at Google Classpath Explorer to help manage the nuts and bolts of doing this.

    It could look something like this:

     ClassPath classpath = new ClassPathFactory().createFromJVM();
     RegExpResourceFilter regExpResourceFilter = new RegExpResourceFilter(".*", ".*\\.class");
     String[] resources = classpath.findResources("", regExpResourceFilter);
    

    resources is an array of Strings like ‘com/foo/bar/Baz.class’. You can now simply loop through and find matching entries, and transform them from slashed to dotted, strip out ‘.class’, etc. Just be careful around trying to match inner classes, as they will have a ‘$’ character in them.

    Also, as far as I am aware, this will NOT cause those classes to be loaded.

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

Sidebar

Related Questions

Given the following object: public class Product { string Name {get;} int Quantity {get;}
given the following class ... public class Category { public string Name {get;set;} public
Given: protected class Marker { public string Name { get; set; } public string
Given the following classes: public class Nation { public string Name { get; set;
Given the following set of classes: class Parent { string Name { get; set;
Given the class: public class Foo { public string Name { get; set; }
Given: case class FirstCC { def name: String = ... // something that will
I have this: one string variable which holds the class name ( $classname )
Given: case class Person(name: String) and trying to do: scala> List(Person(Tom), Person(Bob)).sorted results in
Given a class such as: class Person { private: char *name; public: Person() {

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.