Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Loading a jar that is not on the path

Thread view: 
John Smith - 03 Nov 2005 09:06 GMT
Is it possible to load a jar file (and the classes it contains) that is not
on the class path. I know the disk file, how can I get it loaded?

Thanks

Jon
Roedy Green - 03 Nov 2005 09:30 GMT
On Thu, 3 Nov 2005 08:07:42 -0000, "John Smith"
<usenet123@ntlworld.com> wrote, quoted or indirectly quoted someone
who said :

>Is it possible to load a jar file (and the classes it contains) that is not
>on the class path. I know the disk file, how can I get it loaded?

see http://mindprod.com/jgloss/classloader.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

davidsouther@hotmail.com - 03 Nov 2005 14:23 GMT
In Windows do somehting like:

C:>java -jar C:\path\to\file.jar

or:

C:>path C:\path\to
C:>java -jar file.jar

Of course, you will need to make minor (\ -> : etc.) if you're on a
different OS.

DS
chris_k - 03 Nov 2005 16:00 GMT
Hi,

here is an example:

 static void urlcl() throws Exception {
   URL[] urls = new URL[] {
       new File("C:\\global_libs\\somejar.jar").toURL(),
   };

   URLClassLoader urlCL = new URLClassLoader(urls);
   Class clazz = Class.forName("com.somepackage.CustomStringBuffer",
true, urlCL);
   Object instance = clazz.newInstance();
   System.out.println("toString : " + instance.toString());
   System.out.println("hashCode : " + instance.hashCode());

   Method meth = clazz.getMethod("append", new Class[]
{String.class});
   meth.invoke(instance, new String[] {"test"});

   meth = clazz.getMethod("sendBuffer", new Class[] {Writer.class});
   Writer pw = new PrintWriter(System.out);
   meth.invoke(instance, new Writer[] {pw});
 }

 The best strategy however is to cast the returned instance to some
interface. This allows for re-loading of classes as soon as there is a
new JAR version (you can monitor for JAR file change).
Have a look at the Javadoc of java.net.URLClassLoader.
HTH,
chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.