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

4

10

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

Hampden answered 11/2, 2013 at 22:2 Comment(1)
Look at this question which answered your question. There has been plenty of question on this. #13082420Unclench
P
16

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
Pronator answered 11/2, 2013 at 22:6 Comment(2)
Thank you iamnotmaynard, this worked. I will accept this as the correct response once this website allows me to...Hampden
if java program have more dependence jar files. how to represent the lib path in command prompt.Fulfill
K
5

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.

Kellie answered 11/2, 2013 at 22:5 Comment(1)
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.classKellie
A
0
  1. Open Command Prompt
  2. cd go to the path where jar file exist
  3. run command jar xf fileName.jar
  4. It will generate com, META-INF & Copyright.mk files
  5. Go to the particular package where u want to modify the class file (Basically class files present in com directory)
  6. Download the JAD.exe from google (Its Zip File)
  7. Extract the Zip file you will get jad.exe
  8. Place the class file (which u want to modify) in the folder, that contains jad.exe
  9. Go to command prompt, run jad fileName.class (the class filename which u want to modify)
  10. fileName.jad file will get generate , rename it as fileName.java
  11. compile the fileName.java (if its referring to some other class file configure it in eclipse accordingly)
  12. once compiled replace the class file in the directory defined in step 4
  13. Go to command prompt, cd Step 4 path, run jar cf fileName.jar (* represents all the class files irrespective of directories)
Autotruck answered 2/6, 2016 at 7:3 Comment(0)
S
0
  1. open cmd (windows+r type cmd)
  2. simipily type command as a "jar" without quatoes
  3. it will give some options and those usage.
  4. use what you want
  5. to create a jar file with class you to mention jar cfv filename.jar example.class example2.class etc..
  6. IF your using swing compile it with javac -deprecation SwingApplication.java but JDK version must be greater than version JDK1.1
  7. run java SwingApplication
  8. ALL THE BEst
Supermundane answered 17/6, 2018 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.