Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create jar file with package structure?

Tags:

java

jar

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

like image 991
Anderson Avatar asked Aug 09 '13 12:08

Anderson


People also ask

How do I create a JAR file from multiple packages?

How to create a runnable jar file from multiple java packages. Example java main method file. Lets first compile to java byte-code. Now we are ready to compile / create the jar file.

Is Java package a JAR file?

A JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.)


2 Answers

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).

like image 107
Adam Batkin Avatar answered Oct 05 '22 02:10

Adam Batkin


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.

like image 22
Brian Agnew Avatar answered Oct 05 '22 01:10

Brian Agnew