(JAVA) Use Command Prompt to create .jar file from multiple .class files

75,473

Solution 1

You need to use the entry-point switch -e (with the name of the class containing the main() method) as such:

jar cfve file.jar Main Main.class Main$1.class Main$2.class

Solution 2

Something's gotta tell the java which class should be started automatically. That's the Manifest - see description here You have to package the Manifest.mf in your jar.

Share:
75,473
Skyler
Author by

Skyler

Updated on July 25, 2022

Comments

  • Skyler
    Skyler almost 2 years

    I have written a .java file, called Main.java, and have compiled it using the javac in the Windows Command Prompt. The compiler is creating multiple .class files (called Main.class, Main$1.class, & Main$2.class--presumably because I have anonymous inner classes in my Main.java file). I am trying to create a runnable .jar file so I can double click a shortcut to run this application (it is a Java Swing application), but I am unsuccessful when I navigate to the directory of the three class files and type:

    jar cfv file.jar Main.class Main$1.class Main$2.class
    

    The Command Prompt then outputs this text:

    added manifest
    adding: Main.class(in 4871) (out = 2848)(deflated 41%)
    adding: Main$1.class(in 1409) (out = 833)(deflated 40%)
    adding: Main$2.class(in 1239) (out = 767)(deflated 38%)
    

    Despite this, when I double click on the file.jar file in Windows Explorer, simply put, nothing happens. No swing application opens.

    Hopefully someone can help me out with this. Thank you

    Best...SL

  • michael_s
    michael_s about 11 years
    In your case: add Main-Class: Main to a file Manifest.txt and jar cfv file.jar Manifest.txt Main.class Main$1.class Main$2.class
  • Skyler
    Skyler about 11 years
    Thank you iamnotmaynard, this worked. I will accept this as the correct response once this website allows me to...
  • BasK
    BasK over 9 years
    if java program have more dependence jar files. how to represent the lib path in command prompt.