aspose file tools*
The moose likes Java in General and the fly likes How to locate a class without knowing its package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to locate a class without knowing its package" Watch "How to locate a class without knowing its package" New topic
Author

How to locate a class without knowing its package

Samuel Rizzo
Greenhorn

Joined: Feb 23, 2007
Posts: 6
Hi

Help me please... can't find it out

I need to find a class's full qualified name, knowing just its simple name. Ex.: passing "Class1" to a method and getting its full name "com.pack1.Class1".

Is there a Java API for that?

The problem is:

I'm on a servlet and need to know if a name received as a parameter matches a class (no matter its package) on the application, or, if not, a jsp.

Ex.: parameter received = "Client"
Searches for any Client.class on classpath and discovers its package, or, if not found, any Client.jsp


The web-application may be in a packaged zipped .war or in a exploded directory.


Thanks
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I just did this ... get the classpath from System, for each entry in classpath iterate the files within, when you find a match on name + ".class" try to strip off the directory information and get the package name. I was only looking for classes in my system, so I could find the start of package name by "com" in the fully qualified filename.

You have to handle jars more or less like folders. I did not yet try to handle nested jars; you might have to extract the nested jar. This will mess up if you have the same classname in two packages.

Does that sound fun?

Letting clients invoke any class in your classpath sounds pretty dangerous. Back up to the real business requirement. Is this the best way to do it? I often have the client send in a logical name and look it up in configuration to get a classname. That gives me a bit more control over what the client can run.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
sathish kumar
Ranch Hand

Joined: Feb 14, 2007
Posts: 47
Try using Reflection API's.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can surely use reflection once you have the class, but I don't think it will help you find the class.
sathish kumar
Ranch Hand

Joined: Feb 14, 2007
Posts: 47
posted Yesterday 1:04 PM Profile for Samuel Rizzo Email Samuel Rizzo Send New Private Message Edit/Delete Post Reply With Quote Hi

Help me please... can't find it out

I need to find a class's full qualified name, knowing just its simple name. Ex.: passing "Class1" to a method and getting its full name "com.pack1.Class1".


Stan- From the above quote I think user knows the Class Name. I might be wrong too.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42639
    
  65
I second Stans suggestion to reexamine the requirements.

Also, there is no one-to-one correspondence between packages and classes, since the same class name can be in multiple packages (like "List" in the java class libraries).


Ping & DNS - my free Android networking tools app
Samuel Rizzo
Greenhorn

Joined: Feb 23, 2007
Posts: 6
Thank you all

Indeed, I need to know a list of all user classes with the specified name, at runtime. For example, for "List" it wold return "java.util.List", "java.awt.List" and so on, if those were classes in the web-inf\classes dir. Then, by reflection, I wold know wich one I should use.

A workaround I've just discovered for that, for a while, is running through servletContext.getResourcePaths("/WEB-INF/classes/"), recursively, and "catalogging" all ".class" found, replacing "/" whith "." to know its packages.

I'm just wondering there wasn't a simplier (less ugly) way to do that...


Thank you all, again
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Those who don't understand history are doomed to repeat it. Read this whole page, paying careful attention to "Why the Invoker Servlet is Evil:"

http://faq.javaranch.com/view?InvokerServlet


[Jess in Action][AskingGoodQuestions]
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Originally posted by Ernest Friedman-Hill:
Those who don't understand history are doomed to repeat it. Read this whole page, paying careful attention to "Why the Invoker Servlet is Evil:"

http://faq.javaranch.com/view?InvokerServlet


That was a good read. I didnt know people used to do that. Thanks


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Samuel Rizzo
Greenhorn

Joined: Feb 23, 2007
Posts: 6

Those who don't understand history are doomed to repeat it. Read this whole page, paying careful attention to "Why the Invoker Servlet is Evil:"



Thanks for the good advice. I've read the whole page (and also the link http://tomcat.apache.org/faq/misc.html#evil). Hope not to be on my way to do such an evil thing.

And if a second advice doesn�t hurt, was there a better way to discover the Class's full qualified name?
[ May 18, 2007: Message edited by: Samuel Rizzo ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to locate a class without knowing its package