Solved

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

Posted on 2002-03-24
3
276 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 53
couple of eclipse 5 46
hibernate example using maven 12 59
passing enum to a method 4 23
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

821 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