• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to locate a class without knowing its package

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using Reflection API's.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Samuel Rizzo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Samuel Rizzo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic