Solved

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

Posted on 2002-03-24
3
274 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
Comment Utility
Yes, you can do all of this with a custom classloader.
0
 

Author Comment

by:zollen
Comment Utility
Any step-by-step instruction???

thanks.
0
 
LVL 9

Accepted Solution

by:
doronb earned 100 total points
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now