This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Reflection on files already on the classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reflection on files already on the classpath" Watch "Reflection on files already on the classpath" New topic
Author

Reflection on files already on the classpath

Hector Pertierra
Greenhorn

Joined: Jun 06, 2006
Posts: 25
Hi everyone.

I am doing a little application for executing classes dynamically. I'm already able to test it by loading one class at the time to the classpath at runtime, but now I have to do it by loading all those classes from a JAR.

I have my .class files on a .jar, which is successfully loaded to the classpath at runtime, so I know the classes are there somewhere. Now what I have to do is execute given methods (user enters method names on the UI) on those classes but I don't know where to find them and if it's possible to invoke reflection on them.

Any thoughts??

Thanks in advance.
HP


I'd be useless without Head First books...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yes, it's all possible. Look at java.lang.Class.getMethods(), and the java.lang.reflect.Method class.


[Jess in Action][AskingGoodQuestions]
Hector Pertierra
Greenhorn

Joined: Jun 06, 2006
Posts: 25
Solved. I used antony_miguel's super-duper ClassPathHacker as a base to be able to run loadClass method. I don't really know if it was necessary, but now it works

Just add another method called loadClass and do the same that with addURL

Tnx!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I've used ClassPathHacker, too, but not in production. Using reflection to violate the designer's intent in the core library is not something I'd try to sell my peers on.

I'm not sure why you needed it. You said the jar was "loaded to the classpath at runtime." If that means the jar is in the classpath at JVM startup, you can work with any class in the jar with no more foolin around. All you need is:

Class.forName( fully.qualified.ClassName )


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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reflection on files already on the classpath
 
Similar Threads
Java Reflection
Applying reflection on jar files
Use Reflection to "find" classes
implementation and interfaces in seperate jar files
how discover subclasses of a class?