2011-09-19

To Create a Jar

Creating a .jar file in Java is not as easy as it seems. The flag letters must be in a certain order. Unfortunately, I've not found the documentation for that ordering. This tutorial says the flags may be in any order, but not so in my experience.

After some frustrating trial-and-error, I found the following line works.
jar c0mfv META-INF/MANIFEST.MF whatever.jar source_folders_&_files
To break it down:
  • The 'jar' is the name of the command-line tool
  • 'c' means 'create a jar file'
  • '0' (zero) means no compression
  • 'm' means to use an existing manifest file rather than create one. Omit this to have a manifest auto-generated. 
  • 'f' means send the output of this command to a file rather than to standard output (on the command line).
  • 'v' means 'verbose', to describe each step of the process, displayed on the command line. You can read this to verify, for example, that your pre-existing manifest was copied rather than a new one generated.
  • Pass the relative location of your pre-existing manifest file. Omit if you are omitting the 'm' flag.
  • Pass the name of the jar file to be created. Type the '.jar' on the end yourself. 
  • Pass one or more files and folders to be included. Include the top folder of any packages such as "com" if you have a package such as "com.example.myapp.gui".

Change Directory 

Before typing that 'jar' line, change directory ('cd') to the folder containing the top folder of your package. In other words, you don't jar the folder above your source. From the tutorial linked above, if you have this folder hierarchy:


You would 'cd' into "TicTacToe", and then pass 3 items at the end of your jar comand, for the .class file and 2 folders.

No comments:

Post a Comment