Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-03-24
3
Medium Priority
?
284 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 300 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

824 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