Loading Jar files at runtime

Hi,Would need expert advice, I would like to load jar files in my classpath during runtime. I don't want to load class files individually from jar files. Is it possible to write a code that just load jar files without looking contents in that jar.Thanks,Kapil
[288 byte] By [jar_filesa] at [2007-11-27 6:20:53]
# 1
Errm, yes.
quittea at 2007-7-12 17:36:40 > top of Java-index,Java Essentials,Java Programming...
# 2
If the jar is on your classpath, classes will be loaded from it.
georgemca at 2007-7-12 17:36:40 > top of Java-index,Java Essentials,Java Programming...
# 3

I have the following code that looks jar files in a directory and looks contents in it and then load. Can you pls help me how I can change it to just loop for jar files and those jar files are loaded.

I have the following code in a class that extends URLClassLoader.

public void loadFile() {

try {

URL [] urls = getURLs();

for(int i=0;i<urls.length;i++) {

String path = urls.getPath();

System.out.println("URL PATH :"+path);

JarInputStream jis = new JarInputStream(new FileInputStream(path));

JarEntry entry = jis.getNextJarEntry();

int loadedCount = 0, totalCount = 0;

while (entry != null) {

String name = entry.getName();

if (name.endsWith(".class")) {

totalCount++;

name = name.substring(0, name.length() - 6);

name = name.replace('/', '.');

System.out.print("> " + name);

try {

loadClass(name);

System.out.println("\t- loaded");

loadedCount++;

} catch (Throwable e) {

System.out.println("\t- not loaded");

System.out.println("\t " + e.getClass().getName() + ": " + e.getMessage());

}

}

entry = jis.getNextJarEntry();

}

System.out.println("\n");

System.out.println("Summary:");

System.out.println("\tLoaded:\t" + loadedCount);

System.out.println("\tFailed:\t" + (totalCount - loadedCount));

System.out.println("\tTotal:\t" + totalCount);

}

}

catch(Exception e) {

e.printStackTrace();

}

}

I don't want to do the code in bold. I have some 50 jars in the folder.

Thanks for your help.

Regards,

Kapil

jar_filesa at 2007-7-12 17:36:40 > top of Java-index,Java Essentials,Java Programming...
# 4

You might be looking for something similar to this. This loads a .properties file, but .class files could also be loaded similarly.

import java.io.File;

import java.io.InputStream;

import java.io.IOException;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLClassLoader;

import java.util.Properties;

public class UCLTest {

public static void main (String [] args) {

// Define our jar file with a "something.properties" file in the 'test' package.

File myJar = new File("C:\\test\\testRes.jar");

// Set up a URLClassLoader with the new Jar file in it.

URLClassLoader loader = null;

try {

ClassLoader parentLoader = UCLTest.class.getClassLoader();

loader = new URLClassLoader(

new URL[] { myJar.toURL() }, parentLoader);

} catch (MalformedURLException murle) {

murle.printStackTrace();

}

// Grab the resource out of the jar.

Properties p = null;

try {

InputStream is = loader.getResourceAsStream("test/something.properties");

p = new Properties();

p.load(is);

} catch (IOException ioe) {

ioe.printStackTrace();

}

// Print the property to screen.

System.out.println("My property: " + p.getProperty("MyString") );

}

}

kevjavaa at 2007-7-12 17:36:40 > top of Java-index,Java Essentials,Java Programming...
# 5
Maybe you could explain what you think it means to "load" a jar file.
DrClapa at 2007-7-12 17:36:40 > top of Java-index,Java Essentials,Java Programming...
# 6
Why are you doing this? The JVM will load classes from the classpath for you. What's the bigger picture?
georgemca at 2007-7-12 17:36:40 > top of Java-index,Java Essentials,Java Programming...
# 7

> Why are you doing this? The JVM will load classes

> from the classpath for you. What's the bigger picture?

Only time I've ever needed to do this was for a plugin interface, so that the user could just drop properly created .jar files in the plugins directory and have the application load them all.

It was such a pain in the butt that I eventually just changed the loading script into one that enumerated all the .jar files in the plugins directory and appended them into the classpath.

kevjavaa at 2007-7-12 17:36:40 > top of Java-index,Java Essentials,Java Programming...
# 8

> > Why are you doing this? The JVM will load classes

> > from the classpath for you. What's the bigger

> picture?

>

> Only time I've ever needed to do this was for a

> plugin interface, so that the user could just drop

> properly created .jar files in the plugins directory

> and have the application load them all.

>

> It was such a pain in the butt that I eventually just

> changed the loading script into one that enumerated

> all the .jar files in the plugins directory and

> appended them into the classpath.

You mean using URLClassLoader.addUrl()? Yeh, that's what I did. Not always ideal, though. They'll all belong to the same classloader, for one. Next time you need to do that, use something like OSGi. The Eclipse platform is a good start!

georgemca at 2007-7-12 17:36:40 > top of Java-index,Java Essentials,Java Programming...
# 9

Hi,

Due to performance, I don't want to load every class from jar file. Can I avoid the loop for class file with in the jar file loop. I can just say, from this folder please include all jar files with in this folder in the classpath at runtime. Due to requirement i need to do that in run time, Not at the beginning of starting application where we set the classpath for the application.

Thanks,

Kapil

jar_filesa at 2007-7-12 17:36:40 > top of Java-index,Java Essentials,Java Programming...
# 10

> Hi,

>

> Due to performance, I don't want to load every class

> from jar file. Can I avoid the loop for class file

> with in the jar file loop. I can just say, from this

> folder please include all jar files with in this

> folder in the classpath at runtime. Due to

> requirement i need to do that in run time, Not at the

> beginning of starting application where we set the

> classpath for the application.

>

> Thanks,

> Kapil

Aha. "Performance" is, as usual, a red herring. Just let the JVM load the classes, it's got a far better lazy-loading strategy than you can knock up. No class gets loaded until you need it anyway

georgemca at 2007-7-12 17:36:40 > top of Java-index,Java Essentials,Java Programming...
# 11
> Next time you need to do that, use something like OSGi. Never heard of that, thanks for the tip!
kevjavaa at 2007-7-12 17:36:40 > top of Java-index,Java Essentials,Java Programming...
# 12
Could anyone help me how to put jar files in the classpath at runtime without loading individual class files. I want to put certain jar with traditional classpath setting and certain during runtime that are lying in a directory.
jar_filesa at 2007-7-12 17:36:40 > top of Java-index,Java Essentials,Java Programming...
# 13

> Could anyone help me how to put jar files in the

> classpath at runtime without loading individual class

> files. I want to put certain jar with traditional

> classpath setting and certain during runtime that are

> lying in a directory.

It's not 100% clear what you're after. You can use URLClassLoader to add arbitrary entries to the classpath at runtime, which may or may not be what you're looking for. I can see from your code that you already know how to do this, which leads me to think maybe you're looking for something else, but quite what I don't know. The JVM won't load a class until something tries to use it, or you explicitly do so with either a classloader or Class.forName

georgemca at 2007-7-12 17:36:40 > top of Java-index,Java Essentials,Java Programming...