Search code examples
javaclassjarextractpack

Creating a JAR File from the Command Line Doesn't Work


I've edited this question, and now it is exactly what I did:

I want to create a .jar file from some .class files in the command line.

I worked on Eclipse, and created:

myProject project, and in it: myPackage package, and in it: myClass class. Then I wrote in the command line:

jar -cfv myJar.jar  myPackage\myClass.class

And I got this:

added manifest
adding: myPackage/myClass.class(in = 745) (out= 473)(deflated 36%)

This really created the myJar.jar file in my current directory. Now, I wanted to check if the process was done successfully, so I extracted the class from the jar thus:

jar xfv myJar.jar

And I got this:

  created: META-INF/
 inflated: META-INF/MANIFEST.MF
 inflated: myPackage/myClass.class

And this created for me just the META-INF folder, with the MANIFEST.MF in it, but I don't see any .class file here!!

It seems like something in the packing to jar process is incorrect.

Anybody has an idea??

Any answer is appreciated!


Solution

  • According to the output you gave, there is no myClass.class file in the directory where you execute

    jar -cf myJar.jar myClass.class
    

    So obviously, the command can't add it to the jar: it doesn't exist. If you want to add the myClass directory, recursively, to the jar file, then use

    jar -cf myJar.jar myClass
    

    EDIT:

    Just look at the output:

     inflated: myPackage/myClass.class
    

    The myClass.class file is there in the jar file. There is no problem at all.