Specifying the Main class to run in a jar file from command line

Recently i wanted to run a jar file where its Main entry is not specified in the MANIFEST file. I knew this can be done since i’ve done it before, but couldn’t remember how to do it. So as usual i googled it but could not find it.

The thing is that this is a scenario that can arise when there are multiple entry points (mutiple classes having static main methods) in the jar file.

Funny enough the solution was pretty simple. Eventhough one might think that you need to specify the extry point class and the jar file which it is contained, the truth is we look at that fact in a wrong angle. What we have to do is to specify the class name and then the class path which the jvm should look for relevant classes (thus our jar should be included in this path).

eg:

say we have a class foo.class (which has a static main method) and it is in the jar file bar.jar. Then to execute the jar using the static main method present in foo class

$ java -cp bar.jar foo

here the class name should be a fully qualified class name. You can add more paths to the -cp parameter seprating them from a “:”

Tags: , , , , , , , , , ,

23 Responses to “Specifying the Main class to run in a jar file from command line”

  1. Roman Ovechkin Says:

    Thanks for your good idea!

  2. Lk Says:

    great great solution. thanks.i searched this for my boss at work but wont mind if i have a relationship with u.i jst want to LEARN. u have my email.

  3. dadox Says:

    thanks dude, just what I needed!

  4. Qman Says:

    Thanks. Simple and to the point.

  5. Sean Says:

    Succinct and to the point.

  6. Ragunath Jawahar Says:

    Nice work dude. Thanks, JIT (Just-In-Time) πŸ˜€

  7. Jack Says:

    I’m having a tough time reading your site – the post isn’t being displayed the right way. Might possibly be my web browser (I’m still limited to using firefox 2.5), but I thought I’d give you a heads-up anyway just in case it’s something to do with your theme

  8. Beth Anderson Says:

    This is a very great post about wordpress. I surely love seeing and learning something new about it. Just subscribed and looking forward to read some more articles of yours. Good luck and I hope we can connect in one of these days.

  9. Ocky Says:

    Thanks for your info. I’ve tried and succeed.

  10. fanalion Says:

    Thanks for sharing this πŸ˜€

  11. Mario Says:

    You made my day!!! Thanks!!!

  12. Nandish Says:

    hi thank you very much for sharing this!! i want to learn something more, please send me mails if you find any new things

  13. Dragon warrior Says:

    thanks it helped me…..

  14. Richard Says:

    Could you help me please?…is not working… (I’m in Windows XP):

    C:\>jar -tf test.jar
    META-INF/
    META-INF/MANIFEST.MF
    test/
    test/MyClass.class
    test/MyClass.java

    C:\>java -classpath test.jar MyClass
    Exception in thread “main” java.lang.NoClassDefFoundError: MyClass
    Caused by: java.lang.ClassNotFoundException: MyClass
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: MyClass. Program will exit.

    C:\>

    • samindaw Says:

      It looks like the main class specified is wrong in the Manifest.MF file. Check to see if it is “test.MyClass” not just “MyClass” (the class name should be a fully qualified name)

      HTH

      Saminda

  15. Bob Says:

    Thanks, that was brilliant – just what I needed.

  16. Richard Says:

    Thanks samindaw, – just what I needed.

  17. tony Says:

    it doesn’t work at all
    still can not find main class 😦

    • samindaw Says:

      Hi Tony,
      Whats the error you are getting? Try something simpler like a hello world and see if it works.
      Saminda

  18. Chansoo Lee Says:

    Thank you SOOOO much!!!

  19. Rakesh Says:

    thank you… this was really helpful

  20. Atox Gatal Says:

    Hey, I still got an error even through this process..

    C:\Program Files (x86)\JavaEmulator.com\KEmulator>java -cp KEmulator.jar emulato
    r.Emulator
    Exception in thread “main” java.lang.UnsatisfiedLinkError: C:\Program Files (x86
    )\JavaEmulator.com\KEmulator\swt-win32-3346.dll: Can’t load IA 32-bit .dll on a
    AMD 64-bit platform
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary1(Unknown Source)
    at java.lang.ClassLoader.loadLibrary0(Unknown Source)
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at emulator.k.a(Unknown Source)
    at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
    at org.eclipse.swt.internal.C.(Unknown Source)
    at org.eclipse.swt.widgets.Display.(Unknown Source)
    at emulator.ui.swt.aK.(Unknown Source)
    at emulator.Emulator.main(Unknown Source)

    I tried to run KEmulator in manny ways, but still can’t spot the error.. Please e-mail me asap… πŸ™‚

    • Pico Pron Says:

      I know this is two years late, but here’s the issue: “Can’t load IA 32-bit .dll on a AMD 64-bit platform”.

      Whatever KEmulator is, it referencing a 32-bit dll and you’re running on a 64 bit system. It’s not a java issue.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: