Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loading a classes from a jar file at run time.

Posted on 2002-03-07
6
Medium Priority
?
246 Views
Last Modified: 2013-11-23
Is it possible to load class files from a jar at runtime?  I want to be able to do this without having to put the jar file in the class path.

Thanks
0
Comment
Question by:joegood
[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
6 Comments
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6848768
Sure.  Thre ways I can think of off the top of my head:

1) In applets, the archive="myclsses.jar" attribute in the applet tag does this.  

2) You can place a manifest file in a jar that identifies the class with the 'main' method and that makes the jar executable.  

3) Use the -cp option when runing your class:
java -cp c:\jars\myclasses.jar MainProgram
0
 
LVL 92

Expert Comment

by:objects
ID: 6848852
If your using 1.2 then you can use the URLClassLoader class to load classes that are not in your classpath.
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 600 total points
ID: 6849324
You might also consider the JarClassLoader. The Java Tutorial page describing it is here:
    http://java.sun.com/docs/books/tutorial/jar/api/jarclassloader.html

Jim
0
Industry Leaders: 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!

 
LVL 16

Expert Comment

by:Valeri
ID: 6849847
listening...
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6849886
or - you can easily create you own class loader:

public class CL extends ClassLoader {
 protected String jar;
 public CL(String j) {
     jar = j;
 }
 public Class findClass(String name) {
     byte[] b = loadClassData(name);
     return defineClass(name, b, 0, b.length);
 }

 private byte[] loadClassData(String name) {
     // load the class data from the connection
     try {
         ZipInputStream is = new ZipInputStream(new FileInputStream(jar));
         ZipEntry e;
         while ((e = is.getNextEntry()) != null) {
             if (e.getName().equalsIgnoreCase(name + ".class")) {
                 int r;
                 ByteArrayOutputStream os = new ByteArrayOutputStream();
                 while ((r = is.read()) != -1)
                     os.write(r);
                 return os.toByteArray();
             } else {
             }
         }
     } catch (Exception ex) {
         ex.printStackTrace();
     }
     return null;
 }

// this is the class loading
 public static void main(String args[]) {
     try {
         CL cl = new CL("c:/myTest.jar");
         Class test_class =  cl.loadClass("TheClass");
         Class[] a1 = new Class[1];
         Object[] a2 = new Object[1];
         a2[0] = new Properties();
         a1[0] = a2[0].getClass();
         Method method = test_class.getMethod("run", a1);
         method.invoke(test_class.newInstance(),a2);
     } catch (Exception e) {
         e.printStackTrace();
     }
 }
}

wher the c:/myTest.jar contains this class
public class TheClass {
 public TheClass() {
 }
 public void run(Properties p) {
   System.out.println(p.toString());
 }
}


0
 
LVL 9

Expert Comment

by:Venci75
ID: 7902225
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: objects
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

636 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