Solved

Loading a classes from a jar file at run time.

Posted on 2002-03-07
6
222 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 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

22 Experts available now in Live!

Get 1:1 Help Now