Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

To jar or not to jar?

Get the lowdown on using Java Archive (jar) files -- including pros and cons

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 3 of 5

It decided to make the client classes accessible through a Web server, but not through the server classes. It would have to package pieces of the software separately, and make a decision on how to package each piece.

To run as a trusted applet in the future, the client classes would have to be digitally signed. This meant they would eventually have to be packaged in a signed jar file, a transition that would be greatly eased if the classes were packaged that way from the beginning. Add to that the fact that the software relied on some key technologies in Java 1.1x, meaning backward compatibility had already been given up, and the decision to package the client classes in a compressed jar file was clear.

The JavaBeans were stripped down to the classes necessary at runtime and packaged by themselves in a compressed jar. This would allow an easy swap if the company's JavaBeans were to change. The new jar could be easily specified in the HTML file.

Since the server software would be installed once on the server machine, download time was not an issue. However, keeping all the resources in one file really simplified the install process. Because the server would rarely be restarted, overhead on startup (i.e., decompressing files) was negligible, so Syzygy decided to jar the server resources in compressed format as well. The company could have used a zip file, but going with the jar file meant that any Syzygy resource would be fetched from a jar, thus keeping resource retrieval consistent. This decision proved its merit -- in ease -- when the company had a server resource become a client resource.

Working with jar files

Creating jar files is easy. Packaged with the Java Development Kit (JDK) is a tool named jar. Unix users will immediately notice this tool's similarities to tar, a tape archive tool, but jar files are essentially zip files. In fact, you will find that on the command line, jar can unzip most zip files, and a program like PKZIP or WinZip can unjar jar files. Like a zip file, a jar file can store virtually any other file. The main difference between the zip file and the jar file is the manifest. This is used primarily for JavaBeans to let an IDE know which classes are beans. If you are not creating JavaBeans, you do not need to worry about the manifest file, as the jar tool in the JDK handles this automatically. The most common command for jarring your software is jar cvf. The c tells jar to create, the v tells jar to verbosely tell what it is doing, and f tells jar that the next argument will be the file name you want. Let's say you have your class files packaged under myStuff. You also have some images in an images directory. Your command would look as follows:

      jar cvf myStuff.jar myStuff/*.java myStuff/images/*.gif


You would need to use \ instead of / on a Windows system. By default, the contents of the jar are compressed. If you wanted your files uncompressed, you would just need to add 0 as follows:

    jar cvf0 myStuff.jar myStuff/*.java myStuff/images/*.gif


Now, to see what's in your jar file, you can issue the following command:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • For Sun's JAR Guide, JAR API Reference, and JAR Tools, see http://java.sun.com/products/jdk/1.1/docs/guide/jar/index.html