This isn't exactly a programming question, but it is indirectly related to CLASSPATH errors.
I am unsure of the dependencies for this project. What is a good tool to search for a particular class name inside lots of jars files?
Thank you.
This isn't exactly a programming question, but it is indirectly related to CLASSPATH errors.
I am unsure of the dependencies for this project. What is a good tool to search for a particular class name inside lots of jars files?
Thank you.
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.
I didn't know of a utility to do it when I came across this problem, so I wrote the following:
public class Main {
/**
*
*/
private static String CLASS_FILE_TO_FIND =
"class.to.find.Here";
private static List<String> foundIn = new LinkedList<String>();
/**
* @param args the first argument is the path of the file to search in. The second may be the
* class file to find.
*/
public static void main(String[] args) {
if (!CLASS_FILE_TO_FIND.endsWith(".class")) {
CLASS_FILE_TO_FIND = CLASS_FILE_TO_FIND.replace('.', '/') + ".class";
}
File start = new File(args[0]);
if (args.length > 1) {
CLASS_FILE_TO_FIND = args[1];
}
search(start);
System.out.println("------RESULTS------");
for (String s : foundIn) {
System.out.println(s);
}
}
private static void search(File start) {
try {
final FileFilter filter = new FileFilter() {
public boolean accept(File pathname) {
return pathname.getName().endsWith(".jar") || pathname.isDirectory();
}
};
for (File f : start.listFiles(filter)) {
if (f.isDirectory()) {
search(f);
} else {
searchJar(f);
}
}
} catch (Exception e) {
System.err.println("Error at: " + start.getPath() + " " + e.getMessage());
}
}
private static void searchJar(File f) {
try {
System.out.println("Searching: " + f.getPath());
JarFile jar = new JarFile(f);
ZipEntry e = jar.getEntry(CLASS_FILE_TO_FIND);
if (e == null) {
e = jar.getJarEntry(CLASS_FILE_TO_FIND);
if (e != null) {
foundIn.add(f.getPath());
}
} else {
foundIn.add(f.getPath());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Use a combination of jar
(or unzip
), grep
, and find
.
For example:
for i in *.jar; do jar -tvf "$i" | grep -Hlsi ClassName; done
If you know the entire list of Java archives you want to search, you could place them all in the same directory using (symbolic) links. Otherwise, you would have to change the *.jar
condition accordingly (to use find
).
Or try this search engine:
Or create a graph using:
You can find a class in a directory full of jars with a bit of shell:
Looking for class "FooBar":
LIB_DIR=/some/dir/full/of/jarfiles
for jarfile in $(find $LIBDIR -name "*.jar"); do
echo "--------$jarfile---------------"
jar -tvf $jarfile | grep FooBar
done
Check JBoss Tattletale; although I've never used it personally, this seems to be the tool you need.
Basically let me look at the root of the problem brought up. If you are on a new project - why not come to the PM or technical lead and ask him - how does he track dependencies?
You can use DJ Java Decompiler and the Search for files inside compressed archives tool - enables users to search inside compressed .JAR, .ZIP, .APK, .WAR, .EAR and .EXE archives. This tool enables users to search for files by file name and a word or phrase in the compressed (zipped) file. The result list can be sorted by clicking at column header and includes detailed information about files.
One thing to add to all of the above: if you don't have the jar executable available (it comes with the JDK but not with the JRE), you can use unzip (or WinZip, or whatever) to accomplish the same thing.