Java – How to create jar file with package structure

jarjava

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

Best Answer

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