|
This small java program allows to find where is the .class file for a given class name (with or without the package name)
It looks recursively into subdirectories opening jar and zip files, searching for the class name inside each.
Common calls are:
java FindClass com.acme.AppConfig . // specified class
java FindClass com/acme/config.xml . // specified file
java FindClass StringUtil . // class in any package
...I ve needed this too many times.....
Listing
import java.io.*;
import java.util.*;
import java.util.zip.*;
/**
* @author csilva
*/
public class FindClass {
File root = null;
/** name to search */
String searchEntry;
/** true to search a file without checking the path. */
boolean searchWithoutPath = false;
/** display relative paths */
boolean relativePath = false;
/** debug recursion */
boolean debugRecursion = false;
/**
* Common search: Class (no ".class") com.package.Class folder/file.ext if
* does not contain / then is a class name
*
* @param l
*/
public FindClass(String l) {
if (l.indexOf('/') < 0) { // is a class name
// if not a full class name, seach without path
searchWithoutPath = l.indexOf('.') < 0;
// add .class filename
l = l.replace('.', '/') + ".class";
}
searchEntry = l;
}
void recurse(File f) throws ZipException, IOException {
root = f;
doRecurse(f);
}
void doRecurse(File f) throws ZipException, IOException {
if (debugRecursion && root != f) {
int l = root.getCanonicalPath().length();
String path = f.getCanonicalPath().substring(l + 1);
System.out.println("- Recurse into :" + path);
}
File d[] = f.listFiles();
for (int i = 0; i < d.length; i++) {
if (d[i].isDirectory()) {
doRecurse(d[i]);
} else {
String n = d[i].getName().toLowerCase();
if (n.endsWith(".jar") || n.endsWith(".zip")) {
ZipFile zf = new ZipFile(d[i]);
if (searchWithoutPath) {
for (Enumeration e = zf.entries(); e.hasMoreElements();) {
ZipEntry item = (ZipEntry) e.nextElement();
if (item.getName().endsWith(searchEntry)) {
String path = d[i].getCanonicalPath();
if (relativePath) {
int l = root.getCanonicalPath().length();
System.out.println(path.substring(l + 1) + " : "
+ item.getName());
} else
System.out.println(path + " : " + item.getName());
break;
}
}
} else {
ZipEntry ze = zf.getEntry(searchEntry);
if (ze != null) {
String path = d[i].getCanonicalPath();
if (relativePath) {
int l = root.getCanonicalPath().length();
System.out.println(path.substring(l + 1));
} else
System.out.println(path);
}
}
zf.close();
}
}
}
}
public static void main(String[] args) throws ZipException, IOException {
boolean relativePath = getOption(args, "-r")
|| getOption(args, "--relative-path");
boolean debugRecursion = getOption(args, "-d")
|| getOption(args, "--debug-recursion");
String folder = getArg(args, 1);
String item = getArg(args, 0);
if (item == null) {
System.out.println("FindClass [-r] [-d] <item> [folder]");
System.out.println(" Where:");
System.out.println(" item: Class | package.Class | path/file.ext");
System.out.println(" folder: folder to start search (defaults to .)");
System.out.println(" -r : output relative paths");
System.out.println(" -d : debug recursion");
System.exit(1);
}
if (folder == null)
folder = ".";
FindClass fc = new FindClass(item);
fc.relativePath = relativePath;
fc.debugRecursion = debugRecursion;
fc.recurse(new File(folder));
}
static public boolean getOption(String[] args, String name) {
for (int i = 0; i < args.length; i++) {
if (name.equals(args[i])) {
return true;
}
}
return false;
}
static public String getArg(String[] args, int pos) {
int n = 0;
for (int i = 0; i < args.length; i++) {
if (!args[i].startsWith("-")) {
if (n == pos)
return args[i];
n++;
}
}
return null;
}
}
|