This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma 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

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

Reflection on files already on the classpath

Hector Pertierra

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.

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

Joined: Jul 08, 2003
Posts: 24189

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

[Jess in Action][AskingGoodQuestions]
Hector Pertierra

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

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
Have you checked out Aspose?
subject: Reflection on files already on the classpath
It's not a secret anymore!