How to create jar file with package structure?
JavaJarJava Problem Overview
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
Java Solutions
Solution 1 - Java
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).
Solution 2 - Java
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.
Solution 3 - Java
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
Solution 4 - Java
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.
Solution 5 - Java
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
Solution 6 - Java
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.
Solution 7 - Java
Simply more than above -
-
Keep your Java packaging contents inside a directory and make sure there is nothing inside except your Java packages and their corresponding Java classes.
-
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 .
Solution 8 - Java
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
Solution 9 - Java
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]
Solution 10 - Java
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