I suspect my approach may not be what you want, but it's fairly trivial to create an app that tries to instantiate a java.util.jar.JarFile()
for each *.jar file in the Maven repository.
If there is a problem with that instantiation just capture the name of the jar file and the exception and log it. Obviously you should not be getting any exceptions, so for testing I did a binary edit on a real Jar file just to test the code.
This code was written for Windows. Since it accesses the file system it might need tweaking for other platforms, but apart from that you only need to modify the string specifying the name of the Maven Repository in main()
:
package jarvalidator;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.jar.*;
import java.util.stream.*;
public class JarValidator {
public static void main(String[] args) throws IOException {
Path repositoryPath = Path.of("C:\\Users\\johndoe\\.m2");
if (Files.exists(repositoryPath)) {
JarValidator jv = new JarValidator();
List<String> jarReport = new ArrayList<>();
jarReport.add("Repository to process: " + repositoryPath.toString());
List<Path> jarFiles = jv.getFiles(repositoryPath, ".jar");
jarReport.add("Number of jars to process: " + jarFiles.size());
jarReport.addAll(jv.openJars(jarFiles));
jarReport.stream().forEach(System.out::println);
} else {
System.out.println("Repository path " + repositoryPath + " does not exist.");
}
}
private List<Path> getFiles(Path filePath, String fileExtension) throws IOException {
return Files.walk(filePath)
.filter(p -> p.toString().endsWith(fileExtension))
.collect(Collectors.toList());
}
private List<String> openJars(List<Path> jarFiles) {
int badJars = 0;
List<String> messages = new ArrayList<>();
for (Path path : jarFiles) {
try {
new JarFile(path.toFile()); // Just dheck for an exception on instantiation.
} catch (IOException ex) {
messages.add(path.toAbsolutePath() + " threw exception: " + ex.toString());
badJars++;
}
}
messages.add("Total bad jars = " + badJars);
return messages;
}
}
This is the output from running the code:
run:
Repository to process: C:\Users\johndoe\.m2
Number of jars to process: 4920
C:\Users\johndoe\.m2\repository\bouncycastle\isoparser-1.1.18.jar threw exception: java.util.zip.ZipException: zip END header not found
Total bad jars = 1
BUILD SUCCESSFUL (total time: 2 seconds)