Solved

Loading a classes from a jar file at run time.

Posted on 2002-03-07
6
228 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
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 200 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

911 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

19 Experts available now in Live!

Get 1:1 Help Now