This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes loading classes from jar file at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "loading classes from jar file at runtime" Watch "loading classes from jar file at runtime" New topic
Author

loading classes from jar file at runtime

karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
hi,
follwing scenario:
an app downloads a jar file and creates objects from classes stored in this jar.
how do i load classes from this jar file (the file is not in the classpath -> its not found by the system class loader)
is there a way how i can add a jar file to the classpath dynamicaly ?
or do i have to create a URLClassloader and load all the classes trough reflection ?
thanks
karl
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
You can't dynamically set the classpath at runtime. The classpath system property that is set when starting the VM can be modified at runtime, but the system classloader is already configured with the classpath status at VM start time by then.
You'll have to use a custom classloader to load your classes, or you could use the URLClassLoader you mentioned.
Regards,
G´┐Żnther
http://www.javacoding.net
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
thanks.
i did it with the URLClassloader and it works.
k
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: loading classes from jar file at runtime
 
Similar Threads
How to enumerate jar files and the classes in them?
LOADING CLASS FROM A JAR WHICH IS NOT IN CLASSPATH
Servlets and Package Membership
loading properties into the classpath
some JAR problems.