2

How do I open a .class or .jar file within a Java program? (remember that .jar files may have more than one class with main(String[] args) method)

(individual question from IDE-Style program running )

Community
  • 1
  • 1
Ky -
  • 30,724
  • 51
  • 192
  • 308

5 Answers5

4

Here is a quick and dirty dirty hack for running all main methods found in the jar.

import java.io.*;

class JarRunner {

    public static void main(String[] args) throws IOException,
                                                  ClassNotFoundException {

        File jarFile = new File("test.jar");
        URLClassLoader cl = new URLClassLoader(new URL[] {jarFile.toURL() });
        JarFile jf = new JarFile(jarFile);

        Enumeration<JarEntry> entries = jf.entries();
        while (entries.hasMoreElements()) {
            JarEntry je = entries.nextElement();
            String clsName = je.getName();

            if (!clsName.endsWith(".class"))
                continue;

            int dot = clsName.lastIndexOf('.');
            Class<?> clazz = cl.loadClass(clsName.substring(0, dot));
            try {
                Method m = clazz.getMethod("main", String[].class);
                m.invoke(null, (Object) new String[0]);
            } catch (SecurityException e) {
            } catch (NoSuchMethodException e) {
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }
    }
}

As mentioned by other posters, you may want to have a look in the manifest file for the main class (so you don't have to be guessing). This can be accessed through JarFile.getManifest().

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Hmm, a bit too dirty? I think it will call **all** main methods. A `break` after `m.invoke` could help ;) – Andreas Dolk Oct 29 '10 at 09:36
  • Haha, yea. A bit too dirty :P Thanks. Corrected the text. – aioobe Oct 29 '10 at 09:37
  • Tried to open a simple GUI program I made a while ago ( http://supuh.wikia.com/wiki/Blue_Husky's_Randomizer ) and got a ClassDefNotFoundError for every class it scanned. The problem line was "Class> c = cl.loadClass(clsName.substring(0, dot));" – Ky - Oct 29 '10 at 21:46
  • Ah, ok. Perhaps I should have added `.replace('/', '.')` or something? – aioobe Oct 29 '10 at 21:55
3

The manifest names the jar's entry point.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 1
    It's a zip file. Just open it up and read it. With, like, `java.util.zip`. Then use this: http://snippets.dzone.com/posts/show/3574 – Ignacio Vazquez-Abrams Oct 29 '10 at 08:29
  • 1
    Like any other text file. But right way to operate with jar is using 'java.util.jar' package. There is special Manifest class for processing manifests. – Donz Oct 29 '10 at 08:31
3

Use

java -cp my.jar org.myorg.MyClass

if MyClass is the one you want to start. If my.jar has a proper MANIFEST.MF file indicating MyClass you can use

java -jar my.jar
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
1

You can open a .jar with any compression-software (winrar, winzip, 7zip) and you can run the .class file with java.exe

Tommy
  • 4,011
  • 9
  • 37
  • 59
0

I think your questions is about situation when you don't know specification of external class in compilation time. Am I right?

So you need to use reflection API for creating instance of necessary class and invoking its method. You can see example above.

And for determining class for running from jar file you should use package 'java.util.jar' for accessing manifest via Manifest class. And you can determine entry point of this jar from attribute 'Main-Class'.

Donz
  • 1,389
  • 1
  • 13
  • 21