Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: To jar or not to jar? - JavaWorld - July 1998

To jar or not to jar? - JavaWorld - July 1998

Tutorial Details:

To jar or not to jar?
To jar or not to jar?
By: By Todd A. Webb
Get the lowdown on using Java Archive (jar) files -- including pros and cons
ith browsers that support Java 1.1.x gaining in market share, more developers will be exploring the use of Java Archive (jar) files. Before you open a jar of worms, you should know some of the gotchas involved in using these files. These gotchas can affect most aspects of your project -- from how you write your code, to the service your end-users get.
Jar files are an excellent tool to help overcome some of the hurdles that Java faces, such as packaging software and making a program trusted . They also have drawbacks -- including potentially longer download times, the need to put in extra work to retrieve resources, and a lack of universal support. You need to know the pros and cons and what you want to accomplish with your Java program before you decide whether or not to use jar files.
The history of jar
The Java language makes it easy for the developer to pull together a great many resources and objects for building software. A Java developer can end up with a heavily populated directory structure that often must be made available over the Internet. Anyone familiar with the HTTP protocol knows that a separate HTTP request must be made for every file. This small overhead becomes a big performance issue as the number of files that must be downloaded increases.
From the beginning, a mechanism was needed to simplify deployment of these classes and improve performance. Sun settled first on the zip file format as defined by PKWARE, and the core Java classes are still distributed in zip format. Additionally there was a need for small, modular software components (now known as JavaBeans) that could be packaged into a single file and imported into an integrated development environment (IDE) -- such as Symantec's Visual Café, IBM's Visual Age, or JBuilder by Inprise (the company formerly known as Borland).
These IDEs needed a little more than just a bundle of resources in a zip file, however. They also needed to know more about the classes -- for example, which classes were beans, and which provided support. It was decided that a manifest file would be used for this information, and that the name of the zip file should reflect the availability of the manifest file. A zip file containing the file /meta-inf/manifest.mf was used and dubbed the Java Archive file or jar -- the standard distribution format for JavaBeans.
Jar files also offered a solution to other vexing problems. Java was built on the philosophy that it's better to build an overly secure application and relax security as needed than it is to build a low-security system and try to patch it on demand. The sandbox model used by browsers is very restrictive, and doesn't allow developers to do some things that could be very useful, even very low risk things, such as reading and writing to a single file on the client machine. A mechanism was needed to allow certain code to perform these operations, so the idea of trusted applets was adopted. Since it would be extremely cumbersome to try to mark every class file as trusted, the logical choice was to wrap them all into one file and mark that one file as trusted. That file has the sig extension -- the digitally signed version of jar.
When and how to use jar
To decide whether or not jar is for you, consider the following:
Your target market
Security
Performance
Separate packaging
Target market
The first step in deciding whether to use the jar format is to know your target market. Jar already enjoys universal support from IDEs that deal with JavaBeans. However, jar files are not universally supported by Web browsers. Jar files were introduced with the 1.1 version of Java. A developer should assume that any version of a browser that doesn't run a 1.1 Java virtual machine (JVM) will not understand jar files. A significant number of Internet users still use these older browsers. Many software products also have integrated browsers based on older software -- such as versions of America Online (AOL) and PointCast that use Microsoft's Internet Explorer 3.0. The browsers that currently support jar -- and its digitally signed counterpart, the sig file -- include the latest versions of Netscape's Navigator 4.0x and Sun's HotJava browser 1.1. Even Microsoft appears to fully support jar in its latest incarnation of Internet Explorer 4.0x, even though it has created a proprietary cab file format that serves the same purpose. If you know you need to have backward compatibility with older browsers, you will have to forego using jar files. Sorry.
Security
The next consideration is security. If you're deploying an applet, your code will be restricted to the sandbox model. If you absolutely must have access to the client's file system, you'll have to run your applet as trusted. This requires you to apply a digital signature to a jar file. Your choice has been made.
Performance
The next important consideration is performance. Packaging your software in a jar file can either speed up performance or slow it down. Take, for example, Sun's popular Swing classes, a subset of the Java Foundation Classes (JFC), which are packaged in swingall.jar. Version 1.01 contains 1,305 files compressed to 3,657 kilobytes. Suppose you have an applet that uses swingall.jar -- even if you use only one class from swingall.jar, the entire jar file will be downloaded to the user before that one class will be extracted and loaded. In other cases, with small numbers of classes, it may take more time to get the file, uncompress it, and extract the contents than to simply fetch each file individually. As a general rule, if you have a large number of files and you can keep them in tight packages (just the files you need at runtime) you are better off using jar files for better performance.
Separate packaging
Next, you need to decide if all of your classes should be packaged together. You'll probably want to make some of your code available to the world, release some of it only to a certain group, and restrict the rest of it to the administrator's use. So package your classes separately. You may find that some packages need to be in jar files and others don't.
Consider all of these things when you're deciding on jar usage. Syzygy Technologies Inc., for example, is using jar files to develop a breakthrough computerized time and attendance system. In this new product, a server provides access to an employee database using Java database connectivity (JDBC). The client software allows employees to securely log into the system across the Internet and submit their hours using a Web browser. The client interface requires a large number of files, but Syzygy wanted to keep the start-up time to a minimum. In addition, the company wanted the option of running the client as a trusted applet in a future version without having to change the architecture. Syzygy also used some visual JavaBeans in this product. So, what did Syzygy decide to do?
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 uncompress


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
To jar or not to jar? - JavaWorld - July 1998

View Tutorial:
To jar or not to jar? - JavaWorld - July 1998

Related Tutorials:

Jtrix: Web services beyond SOAP
Jtrix: Web services beyond SOAP
 
Maven ties together tools for better code management
Maven ties together tools for better code management
 
Java for Symmetric Cryptography
Java for Symmetric Cryptography Cryptography—literally, secret writing—is the practice of encrypting and decrypting data. To encrypt or decrypt data, you apply an algorithm, which will be a series of transformations to the input data (the plaintext) to
 
FindBugs, Part 1: Improve the quality of your code
FindBugs, Part 1: Improve the quality of your code One of the problems with code quality tools is that they tend to overwhelm developers with problems that aren't really problems -- that is, false positives. When false positives occur, developers learn
 
Java SMPP API Homepage
Java SMPP API SMPP (Short Message Peer to Peer) is a protocol used by short message entities (SMEs) to communicate with Short Message Service Centres (SMSC, or just SC) for sending an receiving short messages.
 
JEP - Java Mathematical Expression Parser
JEP - Java Mathematical Expression Parser JEP is a Java API for parsing and evaluating mathematical expressions. With this library you can allow your users to enter an arbitrary formula as a string, and instantly evaluate it. JEP supports user defined
 
Introduction to Tag Unit
Getting Started For the purpose of this article, let's say that we would like to test the core taglib from the Jakarta Taglibs implementation of the JSTL, a taglib that many people will be aware of and have experience with. Assuming that you already have
 
Java look and feel icons
These pages contain a collection of toolbar button graphics. The graphics have been designed specifically for use with the Java look and feel. They conform to the Java look and feel Design Guidelines (see the "Designing Button Graphics" section of the "Ap
 
PrEd
PrEd is a Java based graphical utility to find and edit Java property files in JAR, WAR, EAR and other kind of ZIP archives. It is the perfect tool for customizing Java applications, which use XML and Property files for their configuration.
 
SOAP Compression Sender for Apache Axis
Axis can be extended with this transport sender to support the compression of SOAP messages. The sizes of SOAP messages are dramatically reduced resulting in faster transmission over slow network connections. The extension is easy to install and can be us
 
IRC Text to Speech with Java
This article will show you how to create a multi-platform IRC bot (an automated client) that uses the FreeTTS Java speech synthesizer library to convert IRC messages into audible speech.
 
Smokescreen Introduction
Smokescreen is a Java obfuscator. Aside from being able to change symbolic names, it can also modify the bytecode instructions in methods thereby obfuscating control flow. This makes the resulting obfuscated classes much more difficult to decompile.
 
HA-JavaMail: High-Availability JavaMail
HA-JavaMail: High-Availability JavaMail Introduction HA-JavaMail is a JavaMail transport proxy that adds efficiency and reliability to an underlying JavaMail provider. HA-JavaMail is NOT an SMTP implementation - it's a wrapper around an existing imp
 
Using the ASM Toolkit for Bytecode Manipulation
Using the ASM Toolkit for Bytecode Manipulation Sometimes Java developers need to generate or change Java bytecode in the runtime. Is can be necessary for AOP or debugging, or even for performance optimization. There are several frameworks available that
 
A lightweight nonintrusive EJB testing framework
A lightweight nonintrusive EJB testing framework Summary This article presents a simple, easy-to-deploy framework that enables fine-grained tests to be run on the container, making it possible to develop and maintain Enterprise JavaBeans components usin
 
Simple Object Persistence with the db4o Object Database
Simple Object Persistence with the db4o Object Database. db4o has been chosen for applications in embedded systems in which zero administration, reliability, and low footprint are critical features. In Germany, BMW Car IT, for example, uses it in an embed
 
The J2EE Architecture allows the programmers to divide their work into two major categories Business Logic Presentation
The J2EE Architecture allows the programmers to divide their work into two major categories Business Logic Presentation Logic J2EE Architecture The J2EE Architecture allows the programmers to divide their work into two major categories: Business
 
Introduction To Enterprise Java Bean(EJB). Developing web component.
Introduction To Enterprise Java Bean(EJB). Developing web component. Developing web component Introduction To Java Beans J2EE specification defines the structure of a J2EE application. According to the specification J2EE application consists of
 
one-jar
One-JAR is a simple solution to a vexing problem in Java: how to distribute an application as a single jar-file, when it depends on multiple other jar-files. One-JAR uses a custom classloader to discover library jar files inside the main jar.
 
JPackIt JPackIt is a Java application for packaging a Java project into single executable package
Java Project in single jar, class or exe containing all java application resources and referenced libraries.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.