Presents your JAVA E-NEWSLETTER for October 23, 2003 <-------------------------------------------> SEALING YOUR JARS CAN SOLVE SOME OF YOUR CLASSPATH PROBLEMS Many Java-induced headaches are caused by the classpath. One of the problems with the classpath is that multiple versions of the same class or classes can be in the classpath at the same time. The virtual machine will load the first one it finds. There are times, such as when your application is running in a container, when this situation cannot be avoided. Sealed jars offer a partial remedy for this problem. When you use sealed jars in your application, the class loader will ensure that all classes in the same package are loaded from the same jar file. For example, say that class com.acme.A is loaded from the sealed file acme.jar. Later, the class loader is told to load the class com.acme.B. Since A and B are in the same package, the class loader attempts to load class B from acme.jar. If class B cannot be found in acme.jar but can be found elsewhere, then a SecurityException is thrown. Sealing a jar is as simple as providing an entry in the jar's manifest file. To create a sealed jar that contained all of the classes in the com directory and its subdirectories package, here's how the command would look: jar cmf mymanifest.txt myjar.jar com where mymanifest.txt contains: Manifest-Version: 1.0 Sealed: true The whole jar is now considered sealed by the class loaders that use it. Sealing your jars won't solve all of your classpath problems, but it can be helpful. This is also a good habit to develop, so start making sealed jars part of your deployment system. There are more options for sealing; for instance, you can seal some packages and not others. You can find out more here. http://java.sun.com/j2se/1.4.2/docs/guide/extensions/spec.html David Petersheim is a Senior Java Developer with Genscape, Inc. He designs and develops server-side applications to acquire and process real-time energy data. ----------------------------------------