Skip to content

Instantly share code, notes, and snippets.

@swthomas55
Last active May 17, 2017 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swthomas55/af8612fb6c9dd30d23cb36bc5c937fc6 to your computer and use it in GitHub Desktop.
Save swthomas55/af8612fb6c9dd30d23cb36bc5c937fc6 to your computer and use it in GitHub Desktop.
Detect corrupted jar files. Eclipse Maven plugin seems prone to creating these.
import java.util.jar.JarFile;
import java.io.File;
/**
* List corrupted Jar files.
* <p>
* Usage: java JarTest file ...
* <p>
* Prints the name and a message for each corrupted file.
* <p>
* At the end prints the number of good files scanned and the number of bad files found.
*/
public class JarTest {
public static void main(String[] args) {
if(args.length < 1) {
System.err.println("need jar file");
return;
}
int goodCount = 0;
int badCount = 0;
for (String pathname: args) {
try (JarFile file = new JarFile(new File(pathname))) {
file.getManifest();
file.stream().map(e -> true);
// System.out.println(pathname + " OK");
goodCount++;
} catch(Exception ex) {
System.out.println(pathname + " " + ex.getMessage());
badCount++;
// ex.printStackTrace();
}
}
System.out.printf("Good: %d\nBad: %d\n", goodCount, badCount);
}
}
@swthomas55
Copy link
Author

swthomas55 commented Apr 21, 2017

I run this as follows:

cd ~/.m2
# Copy JarTest.java here
javac JarTest.java
find . -name '*.jar' | xargs java JarTest

Typical output when there are no problems is below (with two runs because xargs decided to split the file list into two groups):

Good: 2646
Bad: 0
Good: 622
Bad: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment