(JAVA) Use the command line to create a .jar file from multiple .class files

I wrote a .java file called Main.java and compiled it using javac on the Windows command line. The compiler creates several .class files (called Main.class, Main $ 1.class and Main $ 2.class - presumably because I have anonymous inner classes in the Main.java file). I'm trying to create a .jar executable, so I can double-click the shortcut to launch this application (this is a Java Swing application), but I was unsuccessful when I go to the directory of the three class files and type:

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

The following text is displayed on the command line:

 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, just bet nothing happens. The swing application does not open.

Hope someone can help me with this. Thanks you

Best ... SL

+7
source share
3 answers

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

 jar cfve file.jar Main Main.class Main$1.class Main$2.class 
+12
source

Something should tell java which class should start automatically. This is a manifesto - see the description here. You must pack Manifest.mf in your jar.

+5
source
  • Open command line
  • cd go to path where jar file exists
  • execute jar xf fileName.jar
  • It will generate com, META-INF and Copyright.mk files
  • Go to the specific package where you want to modify the class file (basically the class files present in the com directory).
  • Download JAD.exe from Google (its Zip file)
  • Extract the zip file you get JAD.exe
  • Place the class file (you want to modify) in the folder containing JAD.exe
  • Go to the command line, run jad fileName.clas (the name of the class file you want to change)
  • fileName.jad file will generate, rename it as fileName.java
  • compile fileName.java (if its link to some other class file will configure it accordingly in eclipse)
  • after compilation, replace the class file in the directory specified in step 4
  • Go to the command line, cd Step 4, run jar cf fileName.jar (* represents all class files regardless of directories)
0
source

All Articles