• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading class outside of classpath and running its method ??  RSS feed

 
Serkan Demir
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I am trying to load classes outside of the classpath and use their methods.
I have a field named CURRENT_CLASS in my configuration file. I can load the class but i dont know how to make an object from it and run its method.

I attached the class loading code below. Could you help me?


thanks lot,

Serkan
(PS: current_class field points only Runnable classes and i want to call its run method)
// Create a File object on the root of the directory containing the class file
File file = new File("c:\\myclasses\\");
try {
URL url = file.toURL(); // file:/c:/myclasses/
URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass(getClassFromConf("CURRENT_CLASS");
} catch (MalformedURLException e) {
} catch (ClassNotFoundException e) {
}


[ July 27, 2006: Message edited by: Serkan Demir ]
[ July 27, 2006: Message edited by: Serkan Demir ]
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if it has a no-argument constructor: Class.newInstance()
otherwise: use reflection API (get Constructor from Class Object, invoke constructor)


pascal
 
Serkan Demir
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks pascal, i missed to check its Class API.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!