• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Sample code for loading a class file from a jar file, dynamically.

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need a help in writing a java program which loads a class dynamically.I have a sample.jar program at /home/nisha/sample.jar.

This sample.jar has MySample.class. I want to load Mysample.class dynamically using sample.jar. I want to pass sample.jar through source code and not by setting CLASSPATH env. Something like URI.getResource can be used.

I have idea on how to load a class file. But need help in passing the path of a jar file and loading MySample.class from test.jar

Could somebody help me?

Regards,
Nisha
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you pass the location of the JAR file to your program as a String, you can create a File for it. From the File you can get a URI and from that you can get a URL (going directly to a URL is deprecated in 1.6). Once you have a URL, you can create a URLClassLoader. Once you have a class loader, you can call Class.forName(className, false, classLoader).
 
Marshal
Posts: 80870
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic