aspose file tools*
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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
 
jQuery in Action, 2nd edition
 
subject: loading classes from jar file at runtime