Next page | Contents page |

JAR files

Java archive files are zipped files that contain classes plus other assets such as image files.

Create with the utility jar which comes with the JDK. Eg, the command

	jar  cf  mystuff.jar  *.class  *.png

would collect all classes and PNG images from the current directory and put them into file mystuff.jar.

We can then specify mystuff.jar on the classpath (instead of a directory path) and the JVM will find all classes inside it when it needs them.

JAR files are used widely as a means of distributing packages, classes, etc, including for uploading them to web servers (when they may be called WAR or EAR files - Web or Enterprise).

JAR files are ZIP files

JAR files can be examined with WinZip. The jar utility can in fact be used to make zip files: just give the JAR file the extension .zip.

To make Windows treat a .jar file like a .zip file, so you can explore it: in Windows Explorer, Tools/Folder options, File types tab: find JAR (by typing that) and click Delete button if it is there. Then use the New button, type JAR as the extension and from the Advanced button's long list select "Compressed (zipped) folder".

Distributing an application in a JAR file

  1. Create a text file called mainclass.mf which says which of your classes contains the main() method for starting the application:
    	Main-Class: net.grelf.course.myapp.MyApp
    	[followed by essential blank line]
    
  2. Create a JAR file containing that manifest file and your classes and other assets. Command line:
    	jar -cvfm myjar.jar mainclass.mf *.class *.other *.etc
    
  3. Create a .bat file for running the application, containing the line
    	java -jar myjar.jar
    
  4. Distribute the 2 files: .jar and .bat

The batch file finds everything in the JAR, you don't set a classpath.

Next page | Contents page |