1

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.

InSync
  • 4,851
  • 4
  • 8
  • 30
Mushy
  • 2,535
  • 10
  • 33
  • 54
  • You can't. Simple names aren't unique. Just use fully-qualified names instead of simple names. – Radiodef May 21 '15 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 – Mushy May 21 '15 at 21:49
  • Why do you need to do that? – Radiodef May 21 '15 at 21:50
  • I need to do that because of a reflective dependecy in another widget in which the selected item is obtained as a Java.lang.String and hence constrains my behavior to accept a Java.lang.String. – Mushy May 21 '15 at 21:54
  • 1
    What will you do in the case where two classes in the classloader have the same name, but different packages? – Gus May 21 '15 at 21:57
  • Same name but different packages is not a problem as Java.lang.Class.forName() can still resolve a class object. – Mushy May 21 '15 at 22:01
  • 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. – Gus May 21 '15 at 22:07
  • Does Java.lang.Class.forName(...) return more than one class object? In this case unique class names pulled from a reflective widget leaves me free to ignore this problem. – Mushy May 21 '15 at 22:12
  • 1
    No, Class.forName() only accepts fully qualified class names BECAUSE of this problem. – Gus May 21 '15 at 22:12
  • Am I led to believe there is no way to achieve what I am asking above that I can dynamically discover the fully qualified name of a class represented only as a String in simple class name? – Mushy May 21 '15 at 23:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78481/discussion-between-mushy-and-gus). – Mushy May 21 '15 at 23:22

1 Answers1

8

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(...).

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • I have to do this dynamically and hence will never know what package the class is in. – Mushy May 21 '15 at 21:52
  • 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 May 21 '15 at 23:30
  • @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. – Jesper May 22 '15 at 06:45
  • 1
    @Mushy See for example: [Get all of the Classes in the Classpath](http://stackoverflow.com/questions/3222638/get-all-of-the-classes-in-the-classpath) – Jesper May 22 '15 at 07:07
  • 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. – Mushy May 22 '15 at 11:45