Introduction
When trying to expand a JAR file on the UI, you may encounter the following error message:
Failed to get zip Input Stream: Index -1 out of bounds for length 0
Root Cause:
The root cause is that the JAR file includes a root directory entry “/” inside, which is non-standard and improper. You can check the JAR file using below command:
yanxiuw@yanxiuw-mac % jar -tf strange-root.jar
/
com/
com/test/
com/test/Test.class
Resolution
The resolution is to re-pack a compliant, cross-platform, and runtime-safe JAR file which follows the official Java JAR File Specification. The core rule is to use relative paths and avoid absolute root directories (/). Below are the two standard, recommended methods:
1. Manual Packing with the JDK JAR Command
This is the native way to create a standard JAR file without build tools, and it guarantees no invalid root entry.
Step-by-Step Guide:
1. Navigate to the root directory of your compiled classes
First, cd into the folder that contains your Java package structure (e.g., com/, org/). Do not use absolute file system paths.
# Example: Your classes are under ./my-project/com/...
cd my-project
2. Run the standard jar command
Use the current directory (.) as the source to package all contents with relative paths:
# Create a clean JAR file
jar cvf my-standard-library.jar .
3. Verify the standard structure
Check the JAR contents; you will only see package directories and META-INF, no invalid / root entry:
jar -tf my-standard-library.jar
2. Automated Packaging with Build Tools
For professional Java projects, Maven and Gradle are the industry standards. They automatically generate fully compliant JAR files with zero invalid entries.
1. Follow the standard Maven project structure:
your-project/
├── src/main/java/com/my-maven/api/...
└── pom.xml
2. Run the package command
mvn clean package
3. A standard JAR file will be generated in the target/ folder, with a fully compliant structure.