50

I have a folder structures

/com/cdy/ws/a.class files
/com/cdy/ws/b.class files
/com/cdy/ws/c.class files

When I run the following command “jar cvf asd.jar *.class” it gives jar with all the class files. But the folder structure is not getting generated. All the class files have to be under “com.cdy/ws” but all the classes are in same level of META-INF. Can anyone tell me what is the command to generate the package structure?

Thanks

xyz
  • 21,367
  • 33
  • 107
  • 150
Anderson
  • 930
  • 2
  • 11
  • 16

10 Answers10

99

You need to start creating the JAR at the root of the files.

So, for instance:

jar cvf program.jar -C path/to/classes .

That assumes that path/to/classes contains the com directory.

FYI, these days it is relatively uncommon for most people to use the jar command directly, as they will use a build tool such as Ant or Maven to take care of that (and other aspects of the build). It is well worth the effort of allowing one of those tools to take care of all aspects of your build, and it's even easier with a good IDE to help write the build.xml (Ant) or pom.xml (Maven).

Community
  • 1
  • 1
Adam Batkin
  • 47,187
  • 7
  • 120
  • 110
  • 20
    I was confused by this answer at first. Making it a little more concrete may help others: If your class files are in folder1/folder2/folder3/com/example/test, and you want the package structure to be com.example.test, the command should be `jar cvf program.jar -C folder1/folder2/folder3 .` (The period is part of the command, of course.) – hBrent May 14 '14 at 19:46
  • @Adam Batkin Please could you help me build a jar from this souce code https://github.com/upictec/org.json.me I tried your instructions. I even installed maven , but still can't get the jar :S – eddy May 24 '14 at 01:59
  • @eddy Your project builds a JAR as target/json-1.0.jar - but I couldn't get it to work unless I removed the whole `` tag pair and everything inside them – Adam Batkin May 25 '14 at 01:35
  • 2
    @hBrent What does the last `.` mean? – smwikipedia Dec 20 '14 at 12:56
  • 2
    @smwikipedia It means build the jar out of all of the files at the specified path (akin to using `*.*` in other contexts). If you only wanted to include specific files, I think you could specify filenames or parts of filenames instead of the period. (I think you can, for example, include only files with a certain extension, but since this is not my area of expertise, I'm not sure what the syntax for that would be.) – hBrent Dec 20 '14 at 21:46
  • Is it a problem if I don't follow the file name with `.jar` extension? From Oracle [doc](https://docs.oracle.com/javase/tutorial/deployment/jar/build.html), it says it's not mandatory, but I haven't seen any package without `.jar` extension. – Quazi Irfan Apr 24 '15 at 08:05
  • 1
    @iamcreasy It will work, but it's a good idea to follow the generally accepted convention of ending JAR files with `.jar`. This way you know what the files contain, third-party tools can easily find the JARs, new team members won't be confused, etc... – Adam Batkin Apr 24 '15 at 17:38
17

You want

$ jar cvf asd.jar .

to specify the directory (e.g. .) to jar from. That will maintain your folder structure within the jar file.

Brian Agnew
  • 254,044
  • 36
  • 316
  • 423
12

From the directory containing the com folder:

$ jar cvf asd.jar com
added manifest
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/cdy/(in = 0) (out= 0)(stored 0%)
adding: com/cdy/ws/(in = 0) (out= 0)(stored 0%)
adding: com/cdy/ws/a.class(in = 0) (out= 0)(stored 0%)
adding: com/cdy/ws/b.class(in = 0) (out= 0)(stored 0%)
adding: com/cdy/ws/c.class(in = 0) (out= 0)(stored 0%)

$ jar -tf asd.jar 
META-INF/
META-INF/MANIFEST.MF
com/
com/cdy/
com/cdy/ws/
com/cdy/ws/a.class
com/cdy/ws/b.class
com/cdy/ws/c.class
Daniel
  • 9,414
  • 3
  • 41
  • 61
  • 1
    Thanks.. I got the package structure after running the above command :) – Anderson Aug 09 '13 at 12:39
  • @Daniel Please could you help me build a jar from this souce code github.com/upictec/org.json.me I tried your instructions. I even installed maven , but still can't get the jar :S – eddy May 24 '14 at 02:00
7

Step 1: Go to directory where the classes are kept using command prompt (or Linux shell prompt)
Like for Project.
C:/workspace/MyProj/bin/classess/com/test/*.class

Go directory bin using command:

cd C:/workspace/MyProj/bin

Step 2: Use below command to generate jar file.

jar cvf helloworld.jar com\test\hello\Hello.class  com\test\orld\HelloWorld.class

Using the above command the classes will be placed in a jar in a directory structure.

Unihedron
  • 10,251
  • 13
  • 53
  • 66
Vinayak Dornala
  • 1,339
  • 1
  • 15
  • 22
3

Your specified folderName must be on C:\Program Files\Java\jdk1.7.0_02\bin path Foldername having class files.

C:\Program Files\Java\jdk1.7.0_02\bin>jar cvf program1.jar Foldername

Now program1.jar will create in C:\Program Files\Java\jdk1.7.0_02\bin path

Artjom B.
  • 58,311
  • 24
  • 111
  • 196
Swati Pisal
  • 471
  • 4
  • 5
2

this bellow code gave me correct response

jar cvf MyJar.jar *.properties lib/*.jar -C bin .  

it added the (log4j) properties file, it added the jar files in lib. and then it went inside bin to retrieve the class files with package.

Shib
  • 21
  • 1
0

Simply more than above -

  1. Keep your Java packaging contents inside a directory and make sure there is nothing inside except your Java packages and their corresponding Java classes.

  2. Open Command(If Windows) Prompt, reach to the containing directory path like below -

    C:> cd "C:\Users\UserABC\Downloads\Current Folder"

    C:\Users\UserABC\Downloads\Current Folder>jar cvf hello-world-1.0.1.jar .

ArifMustafa
  • 3,691
  • 4
  • 34
  • 44
0

To avoid to add sources files .java to your package you should do

cd src/
jar cvf mylib.jar com/**/*.class

Supposed that your project structure was like

myproject/
    src/
      com/
        mycompany/
          mainClass.java
          mainClass.class
loretoparisi
  • 12,864
  • 9
  • 78
  • 108
0

This is what I do inside .sh file, let's consider install.sh

#!/bin/sh
echo Installing
cd .../Your_Project_Directory/com/cdy/ws/
jar cfe X.jar Main *.class
cd .../Your_Project_Directory/
ln -s .../Your_Project_Directory/com/cdy/ws/X.jar X
echo Testing...
java -jar X
echo You are Good to Go...Use hapily
#etc.

Creating Executable Jar file at the Class directory and creating a SymLink at anywhere you want.

Run it using,

$ sh install.sh[ENTER]
$ java -jar X[ENTER]
0

Assume your project folder structure as follows :

c:\test\classes\com\test\awt\Example.class

c:\test\classes\manifest.txt

You can issue following command to create a “Example.jar.

jar -cvfm Example.jar manifest.txt com/test/awt/*.class

For Example :

go to folder structure from commmand prompt "cd C:\test\classes"

C:\test\classes>jar -cvfm Example.jar manifest.txt com/test/awt/*.class

shan
  • 173
  • 1
  • 12