Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8302293

jar --create fails with IllegalArgumentException if archive name is shorter than 3 characters

XMLWordPrintable

    • jar
    • 9
    • b04
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      $ java --version
      openjdk 19.0.2 2023-01-17
      OpenJDK Runtime Environment (build 19.0.2+7-44)
      OpenJDK 64-Bit Server VM (build 19.0.2+7-44, mixed mode, sharing)

      $ jar --version
      jar 19.0.2

      $ uname -a
      Linux Nelkinda-Blade-Stealth-2 5.15.0-60-generic #66-Ubuntu SMP Fri Jan 20 14:29:49 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

      $ lsb_release -a
      No LSB modules are available.
      Distributor ID: Ubuntu
      Description: Ubuntu 22.04.1 LTS
      Release: 22.04
      Codename: jammy


      A DESCRIPTION OF THE PROBLEM :
      When creating a jar file, one can specify the name of the Jar file.
      When specifying a jar file name that consists of only 1 or 2 letters, the jar tool crashes with an exception.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Given this source file:
      public class Hello {
          public static void main(String... args) {
              System.out.println("Hello, world!");
          }
      }

      Run these commands:
      $ javac Hello.java
      $ jar cvfe he Hello Hello.class

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The expected result is that the jar tool creates a jar file named "he" in the current working directory, containing the class Hello.class, and containing a META-INF/MANIFEST.MF file with a Main-Class entry for class "Hello".
      ACTUAL -
      The jar tool crashes with an exception:
      java.lang.IllegalArgumentException: Prefix string "he" too short: length must be at least 3
              at java.base/java.io.File.createTempFile(File.java:2152)
              at java.base/java.io.File.createTempFile(File.java:2225)
              at jdk.jartool/sun.tools.jar.Main.createTemporaryFile(Main.java:1741)
              at jdk.jartool/sun.tools.jar.Main.run(Main.java:327)
              at jdk.jartool/sun.tools.jar.Main.main(Main.java:1698)

      There are two possible solutions to this problem.
      Solution 1: Document this in the tool documentation of the jar tool. Bad, this limitation is arbitrary and unnecessary. The problem would remain, and users would have to permanently live with the workarond.
      Solution 2: Simply prefix the temporary file name before using it so it is guaranteed to be long enough.

      BTW it _is_ a use case to create jar files without the .jar suffix. This way, and with Linux binfmt, we can make Java programs look and feel like native Linux binaries to the user.

      ---------- BEGIN SOURCE ----------
      #!/usr/bin/env bash

      cat >Hello.java <<END
      public class Hello {
          public static void main(String... args) {
              System.out.println("Hello, world!");
          }
      }
      END
      javac Hello.java
      jar cvfe he Hello Hello.class
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use a longer name, then rename. Like jar cvfe he.jar Hello Hello ; mv he.jar he

      FREQUENCY : rarely


            jpai Jaikiran Pai
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: