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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:55:35+00:00 2026-06-14T04:55:35+00:00

The reflection classes and methods as well as class loaders etc. need the so

  • 0

The reflection classes and methods as well as class loaders etc. need the so called “binary” names of classes to work with.

The question is, how does one get the binary name if one only has the fully qualified name, i.e. the name that one would use in source code.

For example:

package frege;
public static class RT {
    ....
    public static class X { .... }
}

The fully qualified name of the class would be frege.RT.X. Yet, to get the class object, one needs to write:

Class.forName("frege.RT$X")

and not

Class.forName("frege.RT.X")    // fails with ClassNotFoundException

because X happens to be an inner class of frege.RT.

A possible, but clumsy, solution would be to replace . with $ from backwards, one by one, until Class.forName() doesn’t throw ClassNotFoundException anymore or there are no more . to replace.

Is there any better/well known/standard solution? I looked in the API docs for Class, CLassLoader and java.lang.reflect but did not find anything usable.

  • 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-06-14T04:55:36+00:00Added an answer on June 14, 2026 at 4:55 am

    It now sounds like you want to get the fully qualified name (FQN) from the canonical name. As that is different from working from a simple name I’ll add a second answer.

    The Sun javac command will not compile a classes if a canonical name conflict would result. However by compiling separately you can still get two different classes with the same canonical name.

    An example:

    File src1\com\stack\Test.java

    package com.stack;
    
    public class Test {
        public static class Example {
            public static class Cow {
                public static class Hoof {
                }
            }
        }
    
        public static void main(String[] args) throws Exception {
            Class<?> cl1 = Class.forName("com.stack.Test$Example$Cow$Hoof");
            Class<?> cl2 = Class.forName("com.stack.Test.Example.Cow.Hoof");
            System.out.println(cl1.getName());
            System.out.println(cl1.getSimpleName());
            System.out.println(cl1.getCanonicalName());
            System.out.println();
            System.out.println(cl2.getName());
            System.out.println(cl2.getSimpleName());
            System.out.println(cl2.getCanonicalName());
        }
    }
    

    File src2\com\stack\Test\Example\Cow\Hoof.java

    package com.stack.Test.Example.Cow;
    
    public class Hoof { }
    

    Then to compile and execute:

    set CLASSPATH=
    mkdir bin1 bin2
    javac -d bin1 -sourcepath src1 src1\com\stack\Test.java
    javac -d bin2 -sourcepath src2 src2\com\stack\Test\Example\Cow\Hoof.java
    
    set CLASSPATH=bin1;bin2
    java com.stack.Test
    

    Producing the output:

    com.stack.Test$Example$Cow$Hoof
    Hoof
    com.stack.Test.Example.Cow.Hoof
    
    com.stack.Test.Example.Cow.Hoof
    Hoof
    com.stack.Test.Example.Cow.Hoof
    

    Thus two classes have the same canonical name but different FQNs. Even if two classes have the same FQN and same canonical name, they can still be different if they are loaded via different class loaders.

    To resolve your issue I see several ways forward you could take.

    First you can specify that you match the class with the least amount of nesting and hence the least number of ‘$’s in the FQN. UPDATE It turns out Sun javac does the exact opposite of this and matches the class with the most nesting.

    Second you can test all possible FQNs and throw an exception if there is more than one.

    Third, accept that the only unique mapping is with the FQN then only within a specified class loader and re-work you application appropriately. I find it convenient to use the thread context class loader as a default class loader.

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

Sidebar

Related Questions

Using reflection on Java classes to access all field, methods, and so on: Is
i used java reflections to get methods from a class(loaded those classes).Now i want
In my small project I'm using System.Reflection classes to produce executable code. I need
A contractor where I work is using extension methods to implement CRUD on well-known
I'm using reflection to map getters from one class to setters in another i.e.
I have a non-static C# class with some instance methods, which I need to
I use reflection find all the Classes in my project that Inherit from Packet.Base
I'd like to access some internal classes of the .NET Framework, but using reflection
How can I list all the classes in my current project(assembly?) using reflection? thanks.
In WPF, how do I use reflection to find all classes in a project?

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.