I have a class name which is a String that I can retrieve from the url. I need to get the .class
of this String but I don't know how. I tried to use Class.forName()
but it recuires the full name(package+classname). Knowing that the class is in an other project that is added to the buildpath of the current one! + I couldn't find a way to get this package name! T_T
Any ideas please?
Asked
Active
Viewed 80 times
0
2 Answers
0
I think you will need this Reflection Library Other questions about this: Can you find all classes in a package using reflection?
0
Just append the full name to the class name, I would say.
Class.forName(myOtherPackage + "." + stringFromUrl);
If you don't know the myOtherPackage, it will become trickier, if not impossible, let alone being it 'a good idea' to to this. You could scan the classpath, but what will happen if you find multiple classes with the same name? What will happen if someone types the name of an abstract class?

Erik Pragt
- 13,513
- 11
- 58
- 64
-
I actually don't know the name of "myOtherPackage"..but in the different projects, the classnames are different.so is it possible to get the classpath by scanning it? and how to do this? -thx – M810 May 10 '13 at 14:29
-
Yes, you can. Take a look at [this](http://mcoj.wordpress.com/2009/03/13/classpath-scanning/) – Erik Pragt May 10 '13 at 14:46
-
But i am not using Spring. – M810 May 10 '13 at 15:42
-
Then copy something like [this](https://svn.apache.org/repos/asf/qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/util/ClasspathScanner.java). – Erik Pragt May 10 '13 at 18:08