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
Synchronized Block on Grails Works on Windows But No in Linux
Execute External Program Through Terminal in Java
File.Encoding Has No Effect, Lc_All Environment Variable Does It
How to Avoid Java.Net.Bindexception: Address Already in Use
How to Get Chromedriver Process Pid Using Java
Using Singleton Design Pattern for SQLitedatabase
Practical Limitations of Jvm Memory and CPU Usage
Redirect Java -Version to File or Variable
In Java, "5/0" Statement Doesn't Fire Sigfpe Signal on My Linux MAChine, Why
Sending Command to Java -Jar Using Stdin via /Proc/{Pid}/Fd/0
How to Run Sudo Command Without Password from Java Program
Linux Ulimit with Java Does Not Work Properly
Ugly Fonts in Java Applications on Ubuntu