Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2002-03-24
3
Medium Priority
?
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.
Suggested Courses

719 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