File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reflection on files already on the classpath

 
Hector Pertierra
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's all possible. Look at java.lang.Class.getMethods(), and the java.lang.reflect.Method class.
 
Hector Pertierra
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic