"jar" Command Syntax Rules

Q

What are syntax rules of the "jar" command? I am confused on how to specify different options, like x, v, f, i, etc.

✍: FYIcenter.com

A

The "jar" command usage description is difficult to follow. Here are some "jar" command syntax rules that will help you:

1. The usage description given by entering "jar" with no options is missing the "[-Joption]" option at the end. So the full usage description should be:

Usage: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] 
   [-C dir] files ... [-Joption]

2. The first argument must be a list of option flags with one and only one command option flag, c, t, x, u, or i. Some valid and invalid examples are given below:

Valid jar command syntax:
   jar tf my.jar
   jar ft my.jar
   jar i my.jar
   jar xvf my.jar
   jar c my.class > my.jar
   
Invalid jar command syntax:
   jar f my.jar 
      Missing command option flag
      
   jar ftx my.jar
      Two command option flags

3. The first argument can have zero or some other non-command option flags: v, f, m, 0, M, and e, in any order. Some valid are given below:

Valid jar command syntax:
   jar tf my.jar
   jar tfv my.jar
   jar cfve my.jar Hello Hello.class

4. The first argument can have an optional dash "-" prefix. Some valid examples are given below:

Valid jar command syntax:
   jar tf my.jar
   jar -tf my.jar
   jar cfve my.jar Hello Hello.class

5. If the first argument is "i" or "-i", no other option flags are allowed in the first argument. And the second argument must be the JAR file name. Some valid examples are given below:

Valid jar command syntax:
   jar i my.jar
   jar -i my.jar

6. If f, m, and e flags are specified in the first argument, the second and subsequent arguments are interpreted as one argument for each of these flags in the same order.

Some valid and invalid examples are given below:
Valid jar command syntax:
   jar tf my.jar
   jar cfve my.jar Hello Hello.class
   jar cfvem my.jar Hello my.mf Hello.class
  
Invalid jar command syntax:
   jar tf 
      Missing JAR file name for the f flag

   jar cfvm my.jar
      Missing Manifest file name for the e flag

   jar cfvm my.jar Hello.class
      Hello.class will be interpreted as the Manifest file

7. After the first argument and subsequent arguments for f, m, and e flags, additional arguments are interpreted as inputs to c, t, x, u, or i command option. Some valid examples and invalid are given below:

Valid jar command syntax:
   jar tvf jms.jar javax/jms/XA
      List all classes starts with "javax/jms/XA" in the JAR file

   jar xvf jms.jar javax/jms/XA
      Extract all classes starts with "javax/jms/XA" in the JAR file

   jar uvf my.jar -C classes . -C bin xyz.class
      Add all classes from the "classes" folder 
      and "xyz.class" from the "bin" folder to the JAR file

Invalid jar command syntax:
   jar xvf jms.jar -C classes javax/jms/XA
      -C will be interpreted as the input for the "x" command option
      Nothing will be extracted.

8. As the last argument, the [-Joption] can be specified. Some valid examples are given below:


Valid jar command syntax:   
   jar tvf jms.jar javax/jms/XA -J-Xms2m
   jar tvf jms.jar -J-Xms2m

   jar tvf jms.jar -J-Xms1m
      Valid syntax. But the JVM will reject the -Xms1m option.
      JVM requires more tham 1MB of memory to start

Back to FAQ for JDK JAR (Java ARchive) Tool.

2016-06-03, 2757🔥, 0💬