package jarFinder;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public final class JarFinder {
private static final boolean doExactMatch = true;
private static final String keyname = “snmp”.toLowerCase();
private static final String folderName = “D:/DEV/jboss/common/lib”;
private static final String className = replaceDot(“org.jboss.tm.TransactionManagerLocator”);
private static String replaceDot(String in) {
return in.replaceAll(“\\.”, “/”) + “.class”;
}
private static String replaceSlash(String in) {
return in.replaceAll(“\\\\”, “/”);
}
public List<File> getFiles() {
DirectoryScanner ds = new DirectoryScanner(new File(folderName));
ds.addFilter(new FileFilter() {
public boolean accept(File pathname) {
return (pathname.isDirectory() || pathname.getName().endsWith(“.jar”) || pathname
.getName().endsWith(“.zip”));
}
});
List<File> files = ds.scan();
System.out.println(“Accepted file count :” + files.size());
return files;
}
public static void main1(String[] args) {
JarFinder jf = new JarFinder();
List<File> files = jf.getFiles();
String prefix = “<classpathentry kind=\”lib\” path=\”";
String suffix = “\”/>”;
for (File f : files) {
System.out.println(prefix + replaceSlash(f.getAbsolutePath()) + suffix);
}
}
public static void main(String[] args) throws IOException {
JarFinder jf = new JarFinder();
List<File> files = jf.getFiles();
JarFile jarFile = null;
int hitCount = 0;
if (doExactMatch) {
System.out.println(“Looking for : ” + className);
for (File f : files) {
jarFile = new JarFile(f);
if (jarFile.getEntry(className) != null) {
System.out.println(f.getAbsoluteFile());
hitCount++;
}
}
} else {
System.out.println(“Looking for pattern : ” + keyname);
for (File f : files) {
jarFile = new JarFile(f);
Enumeration<JarEntry> iter = jarFile.entries();
while (iter.hasMoreElements()) {
JarEntry entry = iter.nextElement();
String name = entry.getName();
if (name.toLowerCase(Locale.ENGLISH).indexOf(keyname) > 0) {
System.out.println(f.getAbsoluteFile() + ” -> ” + name);
hitCount++;
}
}
}
}
System.out.println(“Hit count : ” + hitCount);
}
}
July 29, 2010 at 5:12 pm |
Previously, I developed an Intellij plugin for this task. But since there’s such a tool like Maven, one may not need anything to find jar files