Solved

Loading a jar library...... (Is this possible?????????)

Posted on 2002-03-24
3
275 Views
Last Modified: 2013-11-23
Hi Guys,

First question:
  Can java load a jar library file even this jar file is not in the classpath?

Second question:

Two entites
===========
1. A System written in Java
2. Multiple Jar files. Each jar file contains an implementation of a unique java class(s). These java classes are derived from a common base class.

Here is what I want to do:
User does nothing more than including these Jar files in the classpath. The system should be intelligent enough to locate/load all java classes.


Can it be done?
stephen

0
Comment
Question by:zollen
3 Comments
 
LVL 5

Expert Comment

by:nebeker
ID: 6892072
Yes, you can do all of this with a custom classloader.
0
 

Author Comment

by:zollen
ID: 6892151
Any step-by-step instruction???

thanks.
0
 
LVL 9

Accepted Solution

by:
doronb earned 100 total points
ID: 6892253
Hi,


Try using the following code.

MyClassLoader simply uses the URLClassLoader to dynamically load classes from JAR files that are NOT present in the ClassPath. You of course must supply the URL's and class names to these JAR files.

import java.io.*;
import java.net.*;
import java.util.*;

import java.lang.reflect.*;

public class MyClassLoader {
     private Hashtable jarUrlTable;
     private Hashtable classTable;

     public PluginLoader() {
          classTable = new Hashtable();
          jarUrlTable = new Hashtable();
     }

     public Class getDynamicClass(URL jarResourceUrl, String className) throws ClassNotFoundException {
          // Do we have a URLClassLoader for this URL already?
          if (!classTable.containsKey(className)) {
               // Get the CodeBase URL for the Class..
               URL[] piUrl = {jarResourceUrl};
               // Create a new URLClassLoader to load the class..
               URLClassLoader urlLoader = new URLClassLoader(piUrl);
               // Store the URLClassLoader object..
               jarUrlTable.put(jarResourceUrl, urlLoader);
               // Load the Class and store it in the Hashtable..
               classTable.put(className, urlLoader.loadClass(className));
          }
          // Get the stored Class from the Hashtable..
          return (Class)classTable.get(className);
     }
}


Hope this hels,
Doron
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet  URL Rewriting 1 37
map related example 6 49
servlet example 11 40
Java syntax, or is it Selenium 6 14
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question