views:

46

answers:

2

Hi every one!

How can I dynamically load a jar file and list classes which is in it?

+2  A: 

Have a look at the classes in the package java.util.jar. You can find examples of how to list the files inside the JAR on the web, here's an example. (Also note the links at the bottom of that page, there are many more examples that show you how to work with JAR files).

Jesper
Thank you very much, and ".class" files is the classes? how can I load .class files as the classes?
Hossein Margani
So you want to load classes dynamically from a JAR file? That's much more than what you originally asked for. Maybe this question contains some answers: http://stackoverflow.com/questions/194698/how-to-load-a-jar-file-at-runtime
Jesper
ok, thanks I solved it!.
Hossein Margani
+1  A: 

Here is code for listing classes in jar:

import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarList {
    public static void main(String args[]) throws IOException {

        if (args.length > 0) {
            JarFile jarFile = new JarFile(args[0]);
            Enumeration allEntries = jarFile.entries();
            while (allEntries.hasMoreElements()) {
                JarEntry entry = (JarEntry) allEntries.nextElement();
                String name = entry.getName();
                System.out.println(name);
            }
        }
    }
}
YoK