List Contents of Multiple Jar Files

List contents of multiple jar files

You need to pass -n 1 to xargs to force it to run a separate jar command for each filename that it gets from find:

find -name "*.jar" | xargs -n 1 jar tf

Otherwise xargs's command line looks like jar tf file1.jar file2.jar..., which has a different meaning to what is intended.

A useful debugging technique is to stick echo before the command to be run by xargs:

find -name "*.jar" | xargs echo jar tf

This would print out the full jar command instead of executing it, so that you can see what's wrong with it.

Find a class somewhere inside dozens of JAR files?

Eclipse can do it, just create a (temporary) project and put your libraries on the projects classpath. Then you can easily find the classes.

Another tool, that comes to my mind, is Java Decompiler. It can open a lot of jars at once and helps to find classes as well.

Extract several of jar in one command

A simple solution.- Get all the jars and extract it

find ./ -name "*.jar" -exec jar -xf {} \;

How to list the files inside a JAR file?


CodeSource src = MyClass.class.getProtectionDomain().getCodeSource();
if (src != null) {
URL jar = src.getLocation();
ZipInputStream zip = new ZipInputStream(jar.openStream());
while(true) {
ZipEntry e = zip.getNextEntry();
if (e == null)
break;
String name = e.getName();
if (name.startsWith("path/to/your/dir/")) {
/* Do something with this entry. */
...
}
}
}
else {
/* Fail... */
}

Note that in Java 7, you can create a FileSystem from the JAR (zip) file, and then use NIO's directory walking and filtering mechanisms to search through it. This would make it easier to write code that handles JARs and "exploded" directories.



Related Topics



Leave a reply



Submit