Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Loading class outside of classpath and running its method ??

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks pascal, i missed to check its Class API.
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic