Replacing the MANIFEST.MF file in a JAR programmatically

BullyWiiPlaza

I want to modify the MANIFEST.MF after creating the JAR to exclude certain Class-Path entries. For this I decided to use zip4j. Extraction seems to work fine but for putting the MANIFEST.MF file back into the JAR, I use the following 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);

However, this code does not work as expected: The parent directory always ends up being named NF instead of the full META-INF. 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 JARs (which are essentially just ZIPs)?

maven dependency:

<dependency>
    <groupId>net.lingala.zip4j</groupId>
    <artifactId>zip4j</artifactId>
    <version>2.6.1</version>
</dependency>

Furthermore I tried using the jar utility like described here but when invoking the command jar uf MyJAR.jar META-INF/MANIFEST.MF the MANIFEST.MF inside the JAR gets deleted instead of replaced. Using the zip utility via zip -ur MyJAR.jar "META-INF/MANIFEST.MF" works but corrupts the JAR file so it is no longer runnable:

Error: An unexpected error occurred while trying to open file MyJAR.jar
Puce

You can use something like this:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.Manifest;

public class ManifestManipulator {

    public static final void main(String... args) throws IOException {
        if (args.length == 0) {
            throw new IllegalArgumentException("at least the path to the JAR file expected!");
        }
        Path jarPath = Paths.get(args[0]);
        Set<String> attributeNames = args.length > 1 ? new LinkedHashSet<>(List.of(args).subList(1, args.length)) : Set.of();

        if (!attributeNames.isEmpty()) {
            ManifestManipulator manifestManipulator = new ManifestManipulator();
            manifestManipulator.removeManifestEntries(jarPath, attributeNames);
        } else {
            System.out.println("Warning: no entries specified to remove!");
        }
    }

    private void removeManifestEntries(Path jarPath, Set<String> attributeNames) throws IOException {
        System.out.println("Going to remove: " + attributeNames);
        try (FileSystem jarFS = FileSystems.newFileSystem(URI.create("jar:" + jarPath.toUri()), Map.of())) {
            Path manifestPath = jarFS.getPath("META-INF", "MANIFEST.MF");
            Manifest manifest = readManifest(manifestPath);
            Attributes mainAttributes = manifest.getMainAttributes();
            System.out.println("Found main attribute names: " + mainAttributes.keySet());

            boolean removed = mainAttributes.entrySet().removeIf(entry -> attributeNames.contains(((Name) entry.getKey()).toString()));
            if (removed) {
                writeManifest(manifestPath, manifest);
            } else {
                System.out.println("Warning: nothing removed");
            }
        }
    }

    private Manifest readManifest(Path manifestPath) throws IOException {
        try (InputStream is = Files.newInputStream(manifestPath)) {
            return new Manifest(is);
        }
    }

    private void writeManifest(Path manifestPath, Manifest manifest) throws IOException {
        try (OutputStream os = Files.newOutputStream(manifestPath)) {
            manifest.write(os);
        }
    }

}

Please make sure you've added the jdk.zipfs module, which will provide a FileSystemProvider for ZIP/JAR files (see the technote).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Programmatically read Manifest.MF from JAR in an EAR

From Dev

What is the proper way to parse the entries of a manifest.mf file in jar?

From Java

What is the equivalent to JAR file META-INF/MANIFEST.MF for a JIMAGE file?

From Dev

What is the equivalent to JAR file META-INF/MANIFEST.MF for a JIMAGE file?

From Java

Gradle duplicate entry error: META-INF/MANIFEST.MF (Or how to delete a file from jar)

From Dev

Class-Path not getting write to MANIFEST.MF file on jar creation

From Java

Wrong Manifest.mf in IntelliJ IDEA created .jar

From Dev

spring-boot-devtools fails on JAR with missing MANIFEST.MF

From Dev

Specify Manifest.MF during test - Fat / Shaded JAR

From Dev

Specify Manifest.MF during test - Fat / Shaded JAR

From Dev

spring-boot-devtools fails on JAR with missing MANIFEST.MF

From Dev

How to read manifest.mf from *.war/META-INF/MANIFEST.MF file?

From Dev

What is the use of MANIFEST.MF file in Dynamic web project in Eclipse

From Dev

Exclude class path reference from MANIFEST.MF file

From Dev

Grep Exported-Packages from a MANIFEST.MF file

From Dev

Reading Manifest.mf in war file using java

From Dev

Confusion on Uses of Jar Manifest File

From Dev

Gradle Manifest.MF

From Dev

Does the jar MANIFEST.MF max line length of 72 include the EOL bytes

From Dev

Java 'Could not find main class' for .jar even though its written in manifest.mf

From Dev

How to add Classpath attribute in manifest.mf when using spring boot maven plugin to build jar

From Dev

Java 'Could not find main class' for .jar even though its written in manifest.mf

From Dev

Can't update manifest.mf inside .jar: line too long error

From Dev

no main manifest attribute (But I have the manifest file in the jar)

From Dev

Exporting Jar file with Manifest attribute in Android Studio?

From Dev

Using Java to read a .jar manifest file

From Dev

Jar command not adding requested manifest file to archive

From Dev

JAR Manifest file - Difference between Specification and Implementation

From Dev

JAR file manifest does not contain permission attribute

Related Related

  1. 1

    Programmatically read Manifest.MF from JAR in an EAR

  2. 2

    What is the proper way to parse the entries of a manifest.mf file in jar?

  3. 3

    What is the equivalent to JAR file META-INF/MANIFEST.MF for a JIMAGE file?

  4. 4

    What is the equivalent to JAR file META-INF/MANIFEST.MF for a JIMAGE file?

  5. 5

    Gradle duplicate entry error: META-INF/MANIFEST.MF (Or how to delete a file from jar)

  6. 6

    Class-Path not getting write to MANIFEST.MF file on jar creation

  7. 7

    Wrong Manifest.mf in IntelliJ IDEA created .jar

  8. 8

    spring-boot-devtools fails on JAR with missing MANIFEST.MF

  9. 9

    Specify Manifest.MF during test - Fat / Shaded JAR

  10. 10

    Specify Manifest.MF during test - Fat / Shaded JAR

  11. 11

    spring-boot-devtools fails on JAR with missing MANIFEST.MF

  12. 12

    How to read manifest.mf from *.war/META-INF/MANIFEST.MF file?

  13. 13

    What is the use of MANIFEST.MF file in Dynamic web project in Eclipse

  14. 14

    Exclude class path reference from MANIFEST.MF file

  15. 15

    Grep Exported-Packages from a MANIFEST.MF file

  16. 16

    Reading Manifest.mf in war file using java

  17. 17

    Confusion on Uses of Jar Manifest File

  18. 18

    Gradle Manifest.MF

  19. 19

    Does the jar MANIFEST.MF max line length of 72 include the EOL bytes

  20. 20

    Java 'Could not find main class' for .jar even though its written in manifest.mf

  21. 21

    How to add Classpath attribute in manifest.mf when using spring boot maven plugin to build jar

  22. 22

    Java 'Could not find main class' for .jar even though its written in manifest.mf

  23. 23

    Can't update manifest.mf inside .jar: line too long error

  24. 24

    no main manifest attribute (But I have the manifest file in the jar)

  25. 25

    Exporting Jar file with Manifest attribute in Android Studio?

  26. 26

    Using Java to read a .jar manifest file

  27. 27

    Jar command not adding requested manifest file to archive

  28. 28

    JAR Manifest file - Difference between Specification and Implementation

  29. 29

    JAR file manifest does not contain permission attribute

HotTag

Archive