2

I have a String variable that keeps a name of Java Class (let's call it Dog) and I use this string like this:

String myString = className; Class<?> klass = Class.forName(myString);

However, this throws a ClassNotFoundException because my Dog is in a package, and I suppose Class.forName wants the full name (my.project.the.package.path.Dog). Is there a way to get the full package name so I can pass it to Class.forName()?

Thanks!

yu_sha
  • 4,290
  • 22
  • 19
  • What is the value of `myString`? Where does it come from? –  Jul 10 '15 at 13:38
  • the value of myString comes from a URL. I think that's irrelevant. For the purpose of this, let's just assume String myString = "Dog". When I call Class.forName(myString) aka Class.forName("Dog"), i get an exception saying Dog is not found, because Dog is in the package my.project.blah.blah.blah.Dog. I need that part of the path before Dog so I can pass forName the entire path. – Adrian Daniel Culea Jul 10 '15 at 13:41
  • "Part of path before Dog" is called Java package. – yu_sha Jul 10 '15 at 13:42
  • Well, do you have any clear way of knowing in which package the class is supposed to be? Because there could be a thousand "Dog" classes in many different packages in your class path. – RealSkeptic Jul 10 '15 at 13:42
  • There is only one Dog class in my entire project and I need to know its path. – Adrian Daniel Culea Jul 10 '15 at 13:44
  • That sounds like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). This isn't a situation that should usually arise. So maybe you should explain the background to this and why you *think* you need this. We may be able to help with the *real* problem. – RealSkeptic Jul 10 '15 at 13:49

4 Answers4

1

If all you have is Dog it is impossible for the JVM to know which class is mean. Consider this.

package com.example.foo;

class Dog {
}

and

package com.example.bar;

class Dog {
}

How shall the JVM know if com.example.foo.Dog or com.example.bar.Dog is meant by Dog? The full name including packages is necessary.

  • I know this might be a scenario. I only have one single class named Dog in my entire project, but I don't know its path and I need it so I can pass it to forName – Adrian Daniel Culea Jul 10 '15 at 13:45
  • Then you would have to scan all registered classes and find the one named `Dog`. Than *can* be done but I recommend to fix the source of your problem. –  Jul 10 '15 at 13:46
0

You cannot do this because there may be more than one Dog classes in various packages.

But you can iterate through all loaded classes using approach given in one of the answers to this question

See answer by eis. You can use this code to list all classes that have been loaded, and find the one or the ones that are called "Dog".

However, you probably should not do this. Wherever you get this string from - either you should have default package name or pass complete class name.

Community
  • 1
  • 1
yu_sha
  • 4,290
  • 22
  • 19
  • The client would just type Dog in a web page, I get that name and I have to do some magic involving a Dog object. Obviously, the client won't know how to give me the full path, just the name of the class. – Adrian Daniel Culea Jul 10 '15 at 13:49
  • Then see the example in http://stackoverflow.com/questions/2548384/java-get-a-list-of-all-classes-loaded-in-the-jvm – yu_sha Jul 10 '15 at 14:58
0

Store class name with path using class.getCanonicalName() and then access it using Class.forName

String classPath = Dog.class.getCanonicalName();
System.out.println("Store class path:" + classPath);

try {
    Class<?> klass = Class.forName(classPath);
    Dog objDog = (Dog) klass.newInstance();
    System.out.println(objDog);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (InstantiationException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
}
Vicky Thakor
  • 3,847
  • 7
  • 42
  • 67
  • I don't know the client selected Dog, per say. What I have is a String that has the value "Dog". How do I get the path of the Dog class? What you suggest is true, but it doesn't quite apply to my scenario – Adrian Daniel Culea Jul 10 '15 at 14:58
-1

You creating Dog.class not object. If you want get class info then you must put full class name with package.

System.out.println( Dog.class.getName() );
Class< ? > c = Class.forName( Dog.class.getName() );
/// Class< ? > c = Class.forName( "com.some.package.Dog" );
Dog t = (Dog) c.newInstance();
Robert Wadowski
  • 1,407
  • 1
  • 11
  • 14
  • I only have the Test part of that String. The package that contains the Test class may be unknown, therefore, I cannot hardcode it there. I need a way to find it writing a piece of code. – Adrian Daniel Culea Jul 10 '15 at 13:43
  • 1
    With no package info you won't get it – Robert Wadowski Jul 10 '15 at 13:45
  • What do you mean by "no package info" – Adrian Daniel Culea Jul 10 '15 at 13:49
  • I did not write anything about package info, I wrote about package name where class is declared. Sorry for misleading comment – Robert Wadowski Jul 10 '15 at 13:51
  • I cannot do `Dog.class.getName()` because I don't know what the client typed in the browser. I only have this as a String variable and I'm trying to create an instance of it, but to do so I need the full path. – Adrian Daniel Culea Jul 10 '15 at 13:56
  • Or provide it from somewhere else, if you saying that you are sure where is class located try hardcode "com.some.package." + className, put package name to some constant etc. However it is not good solution. Btw. this is assumed that Dog.class.getName() = "com.some.package.Dog" for example needs – Robert Wadowski Jul 10 '15 at 13:59