?
Solved

Loading a classes from a jar file at run time.

Posted on 2002-03-07
6
Medium Priority
?
243 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month12 days, 3 hours left to enroll

752 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