Solved

Loading a classes from a jar file at run time.

Posted on 2002-03-07
6
230 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 49
Apps blocked by Java 9 79
map related example 6 48
tomcat administrtor 12 47
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

773 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