|Jar vs Zip||Writing To Jars|
|Tips on Using Jar Files||Gotchas|
|Building Jar Files||Sealing|
|Executing Jar Files||Nesting|
|Making Jar Files Double Clickable||jar.exe|
|Manifest Gotchas||Jars On Mac|
If the jar is digitally signed there is the possibility of Applets escaping the restrictions of the sandbox. You don’t need to sign your jar files if your Applets are willing to live with the usual security restrictions. Since Netscape, Microsoft and Sun use incompatible signing techniques, you may have to sign your jar file multiple times or have separate jars for each of the bewildering array of signing and security schemes. To make matters worse, Internet Explorer ignores signing in jar files. It only pays attention to it in cab files. When the elephants fight, it is the grass that suffers. See signed Applets for more information. Today, the solution is to ignore Microsoft-style signing and the Microsoft Java implementation. When you install Sun Java in IE (Internet Explorer), it will use Sun-style signing.
Class.getResource is broken, in that Netscape 4.0+ does not support it. Now that Netscape 8.0 is available, perhaps we can ignore that restriction.
To get information out of jar files other than the main jar, you would need to use ZipFile and ZipEntry.getInputStream.
To see how to fish *.gifs and jpgs out of jar files, see Image.
If you are using packages, for jar.exe you must be in the root directory when using jar.exe to ensure your package names get properly included because the names you give jar.exe on the command line are the names it will blindly use for your packages inside the created jar file. You don’t want any names like C:\com\mindprod\mypackage.MyClass.class or \com\mindprod\mypackage.MyClass.class or MyClass.class (unless you have no packages). You want names like com\mindprod\mypackage\MyClass.class which translates to com.mindprod.mypackage.MyClass.class on the command line. They have to make sense both as filenames and as fully qualified package/class names.
If you are not using packages, you must be in the directory where the class files are when you build your jar. That way the filename MyClass.class will be the same as the classname MyClass.class.
HereSee Main-Class for details on how to create the main.mft file. See jar.exe for another explanation of how to use it. You create a main.mft file to help generate an /META-INF/MANIFEST.MF member. You don’t create the /META-INF/MANIFEST.MF directly.
Starting in Java version 1.5, you can super-compress jar files using pack200.exe. This will optionally remove debug information, and compress the jar as a whole.
One of the most useful associations to set up is to make *.jar files executable with java.exe (see below for details how). Then you can just type the name of the jar on the command line to start it executing. For Java version 1.4 or later, after you have built the Main-Class entry, and set up an association of the *.jar extension to C:\Program Files\java\jre7\bin\java.exe, all you need to do to run the jar is:
java.exe -jar converter.jar
For Java version 1.1, you need a little more, something like this:
To run the jar in an Applet, see Applet for some sample HTML (Hypertext Markup Language) or see
on doing it.
Manifest-Version: 1.0 Created-By: 1.4.0 (Sun Microsystems Inc.) Main-Class: com.mindprod.bio.BiorhythmsA Sun signed version is more complex in that it also contains a hash for each member put here by jarsigner.
Manifest-Version: 1.0 Created-By: 1.4.0 (Sun Microsystems Inc.) Main-Class: com.mindprod.bio.Biorhythms Name: com/mindprod/bio/Biorhythms.class SHA1-Digest: ueEw1mJ4aOXT9vmosR0nM/eUt6Y= Name: com/mindprod/bio/SelectableDate.class SHA1-Digest: 8HylCo90The8D1mBuYEEOFQDUsY=A Netscape signed version is more complex still in that in has two different hashes for each member put there by signtool. The specification document is very loose about the format of a manifest file. Here are the rules I have gleaned from experimentation:
Main-Class: com.mindprod.MyProgram Class-Path: activation.jar axis.jar commons-discovery.jar commons-logging.jar jaxrpc.jar log4j-1.2.8.jar mail.jar saaj.jar junit.jarI suggest putting long names on a line by themselves, with a single space on the start of the line, and using short names to stay away from the 72 limit. Be very careful with spaces. Don’t add extra ones or remove any. jar.exe is highly senstive to having its spaces and blank lines just right. It does not give helpful error messages.
java.exe -jar biorhythms.jarThe key is an attribute line in the meta-inf/manifest.inf manifest member of the form:
You can also specify the classpath, but you cannot specify command line parameters or system properties in the manifest.
Netscape 4.79’s signtool.exe won’t just sign an existing jar file. You have to take your existing jar apart into separate files, sign it, and let it recreate the jar file. Beware: the signtool -m option uses an incompatible format for the main.mft files that differs jar.exe’s. A signtool-style main.mft file needs a lead + sign on its entries, e. g.
According to Sun’s JWS (Java Web Start) FAQ (Frequently Asked Questions), in Java version 1.5, Java Web Start still does not support Class-Path. There was a report that up until Java version 1.5, java.exe too ignored this entry, however there is another report it has always worked fine. Class-Path is tricky to use, possibly leading to the erroneous report. Note the list is space-separated, not semicolon-separated as in the SET CLASSPATH=C:\;. Note also that only relative directories and jar names are permitted using / not \. They are relative to the containing jar. You cannot use C:\. Note also that directory names need to terminate (but not begin) with a /. This is because Class-Path has to be platform-independent.
Note that jars always ignore the SET classpath and even the command line classpath. These other two classpaths are in no way merged with the manifest Class-Path.
If you use -jar on the java.exe command line, java.exe will quietly ignore the set environment classpath and any -classpath or -cp command line options. What are you to do if you have additional jars to include?
Extension-List: activation mail activation-Extension-Name: javax.activation activation-Implementation-URL: http://abc.com/activation.jar mail-Extension-Name: javax.mail mail-Implementation-URL: http://abc.com/mail.jar
You can also read and write the jar files from Java with the ZipEntry, ZipException, ZipFile, ZipInputStream and ZipOutputStream classes. While a jar is in use, some OS (Operating System) ’s may lock it, so don’t count on being able to update jars on the fly when you are executing classes from them.dependencies, how to make sure you include everything you need in your jar — your classes, all the classes they use, and all the classes they in turn use.
You have a similar problem with packing zip files of classes and source.
When you recompile, naturally this does not magically update all the jars in the world. Similarly when you build a jar, jar.exe will happily include out of date class files that really should have been recompiled. Make sure everything, including classes from other packages have been freshly compiled before building your jar. Here is my plan for preparing a distribution jar:
To seal a package, you need to add a Name header for the package to your manifest, followed by a Sealed header, like this:
There are three approaches to the problem:
You create jars on the Mac with the Jar Bundler, (not jar.exe). It creates and includes an XML (extensible Markup Language) file called Info.plist that behaves something like a JNLP (Java Network Launching Protocol) file does for a Java Web Start App.
available on the web at:
optional Replicator mirror
Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[22.214.171.124]
|Feedback||You are visitor number 447,826.|