title: Replacing the MANIFEST.MF file in a JAR programmatically
body: <p>I want to modify the <code>MANIFEST.MF</code> after creating the <code>JAR</code> to <a href="https://stackoverflow.com/questions/62173831">exclude certain <code>Class-Path</code> entries</a>. For this I decided to use <a href="https://github.com/srikanth-lingala/zip4j" rel="nofollow noreferrer"><code>zip4j</code></a>. Extraction seems to work fine but for putting the <code>MANIFEST.MF</code> file back into the <code>JAR</code>, I use the following code:</p>
  
  <pre><code>String metaInfFolderName = "META-INF";
  Path extractedManifestFilePath = Paths.get("...");
  ZipFile zipFile = new ZipFile("Test-Zip-File.zip");
  ZipParameters zipParameters = new ZipParameters();
  zipParameters.setDefaultFolderPath(metaInfFolderName);
  zipFile.addFile(extractedManifestFilePath.toFile(), zipParameters);
  </code></pre>
  
  <p>However, this code does not work as expected: The parent directory always ends up being named <code>NF</code> instead of the full <code>META-INF</code>. It seems like the starting characters are cut off. What could be the reason for this or is there another meaningful possibility to replace files inside <code>JAR</code>s (which are essentially just <code>ZIP</code>s)?</p>
  
  <p><code>maven</code> dependency:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;net.lingala.zip4j&lt;/groupId&gt;
      &lt;artifactId&gt;zip4j&lt;/artifactId&gt;
      &lt;version&gt;2.6.1&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Furthermore I tried using the <code>jar</code> utility like described <a href="https://docs.oracle.com/javase/tutorial/deployment/jar/update.html" rel="nofollow noreferrer">here</a> but when invoking the command <code>jar uf MyJAR.jar META-INF/MANIFEST.MF</code> the <code>MANIFEST.MF</code> inside the <code>JAR</code> gets deleted instead of replaced. Using the <code>zip</code> utility via <code>zip -ur MyJAR.jar "META-INF/MANIFEST.MF"</code> works but corrupts the <code>JAR</code> file so it is no longer runnable:</p>
  
  <pre><code>Error: An unexpected error occurred while trying to open file MyJAR.jar
  </code></pre>