2

I have considered in thought how I can take a java.lang.String representing a class name and determine the fully qualified name to then convert the string to a class using java.lang.Class.forName(FullyQualifiedName).

This is puzzling me because I can't use java.lang.Class.forName() with a java.lang.String representing a class name such as "Address" because I need the fully qualified name or I get an Exception.ClassNotFoundException which cycles me back to needing to know the fully qualified name but how when I can't use java.lang.Class.forName().

I am simply stuck with my current knowledge. I have searched using Google and even with the creation of this question but I bet this has already been asked-n-answered. Someone please point me in the right direction?

Note: This question should be removed as it does not appear possible to do what the OP is requesting in Java.

11
  • You can't. Simple names aren't unique. Just use fully-qualified names instead of simple names. Commented May 21, 2015 at 21:43
  • But I need to determine the fully qualified name dynamically from a simple Java.lang.Class name represented as a Java.lang.String Commented May 21, 2015 at 21:49
  • 1
    What will you do in the case where two classes in the classloader have the same name, but different packages? Commented May 21, 2015 at 21:57
  • 1
    My point is, how will you decide which is the correct one to pass off to Class.forName()? They're both classes, and they both have name foo. Is com.poo.foo the one you want, or org.poo.foo? No program can answer that for you; you're asking it to guess. Commented May 21, 2015 at 22:07
  • 1
    No, Class.forName() only accepts fully qualified class names BECAUSE of this problem. Commented May 21, 2015 at 22:12

1 Answer 1

14

The fully qualified name of a class is the name of the class prefixed with the package name. For example if class Address is in a package com.mycompany.myproject, then the fully qualified name of class Address is com.mycompany.myproject.Address.

Java can only find classes by their fully qualified name (unless the class is in the default package - but then the simple name of the class is also the fully qualified name).

You need to know in what package your class is in, otherwise there is no way to load the class with Class.forName(...).

Sign up to request clarification or add additional context in comments.

7 Comments

I have to do this dynamically and hence will never know what package the class is in.
I agree with you are writing in your answer but this is not an aswer to my question in which I am using a simple class name in the form of a Java.lang.String and want to discover the fully qualified name of the class and get a class object through Java.lang.Class.forName(...)
@Mushy Then you will have to write code that searches through all packages on the classpath to find the class with the simple name that you are looking for. That is probably not easy, and you'll have to decide what should happen if there are multiple classes with the same simple name in different packages.
There won't be multiple classes with same name in different packages. I have the afforded luxury to work within a namespace that is singularly unique in class name but not so in field name. Only issue I have with searching is response time but I will do it. This answer is more to what I was looking for and will be accepted if no other solutions present themselves.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.