鉴于班级名称:com.example.MyClass
(或者用' /
代替)我如何快速确定包含一堆广口的文件夹中是否存在java类/ class files?
目前我唯一的解决方案是使用存档检查器(7zip)打开它们并搜索目录。
比这更好的事情会非常有帮助。
编辑:此外,我正在寻找比在IDE中创建新项目更快的内容,添加文件夹并使用它来查找课程。
更新:如果此问题没有得到有用的答案,我{m} building a utility called javaclassfinder还有一个问题需要评论' README.md中的部分,人们可以帮助我回答构建实用程序所需的一些问题。也欢迎您发表评论并提出实施方法!
更新: user3819021的解决方案很有帮助,虽然它取决于cygwin的存在,但如果有人知道只有Windows的解决方案,我们将不胜感激。
答案 0 :(得分:2)
如果您的计算机上安装了cygwin,则可以尝试以下代码:
find <your root path> -name '*.jar' | while read file; do unzip -l "$file" | grep -q <search file> && echo $file; done
<search file>
应该是/path/YourClass.class
或YourClass.class
。
答案 1 :(得分:0)
打开PowerShell提示符并运行
gci -LiteralPath "C:\work\src\javastuff" -Filter *.class | ? { $_.FullName -match "my.package.SomeClass.class" }
- 如果您只想要一个完整路径和文件名列表,请添加以下内容:
| % { $_.FullName }
答案 2 :(得分:0)
以下代码将搜索特定类的整个类路径。如果没有参数,它将转储它找到的每个类,然后你可以管道grep或重定向到一个文件。它看起来在罐子里......
用法:WhichClass
或WhichClass package.name
(请注意没有.class
)
对缺乏评论表示道歉......
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public final class WhichClass {
private WhichClass() {
}
static Vector<String> scratchVector;
public static void main(final String[] argv) {
Vector v;
if ((argv.length == 0) || "-all".equals(argv[0])) {
v = findClass(null);
} else {
v = findClass(argv[0]);
}
for (int i = 0; i < v.size(); i++) {
System.out.println(v.elementAt(i));
}
}
static String className(final String classFile) {
return classFile.replace('/', '.').substring(0, classFile.length() - ".class".length());
}
static Vector findClass(final String className) {
if (className != null) {
scratchVector = new Vector<String>(5);
} else {
scratchVector = new Vector<String>(5000);
}
findClassInPath(className, setupBootClassPath());
findClassInPath(className, setupClassPath());
return scratchVector;
}
static void findClassInPath(final String className, final StringTokenizer path) {
while (path.hasMoreTokens()) {
String pathElement = path.nextToken();
File pathFile = new File(pathElement);
if (pathFile.isDirectory()) {
try {
if (className != null) {
String pathName = className.replace('.', System.getProperty("file.separator").charAt(0)) + ".class";
findClassInPathElement(pathName, pathElement, pathFile);
} else {
findClassInPathElement(className, pathElement, pathFile);
}
} catch (IOException e) {
e.printStackTrace();
}
} else if (pathFile.exists()) {
try {
if (className != null) {
String pathName = className.replace('.', '/') + ".class";
ZipFile zipFile = new ZipFile(pathFile);
ZipEntry zipEntry = zipFile.getEntry(pathName);
if (zipEntry != null) {
scratchVector.addElement(pathFile + "(" + zipEntry + ")");
}
} else {
ZipFile zipFile = new ZipFile(pathFile);
Enumeration entries = zipFile.entries();
while (entries.hasMoreElements()) {
String entry = entries.nextElement().toString();
if (entry.endsWith(".class")) {
String name = className(entry);
scratchVector.addElement(pathFile + "(" + entry + ")");
}
}
}
} catch (IOException e) {
System.err.println(e + " while working on " + pathFile);
}
}
}
}
static void findClassInPathElement(final String pathName, final String pathElement, final File pathFile)
throws IOException {
String[] list = pathFile.list();
for (int i = 0; i < list.length; i++) {
File file = new File(pathFile, list[i]);
if (file.isDirectory()) {
findClassInPathElement(pathName, pathElement, file);
} else if (file.exists() && (file.length() != 0) && list[i].endsWith(".class")) {
String classFile = file.toString().substring(pathElement.length() + 1);
String name = className(classFile);
if (pathName != null) {
if (classFile.equals(pathName)) {
scratchVector.addElement(file.toString());
}
} else {
scratchVector.addElement(file.toString());
}
}
}
}
static StringTokenizer setupBootClassPath() {
String classPath = System.getProperty("sun.boot.class.path");
String separator = System.getProperty("path.separator");
return new StringTokenizer(classPath, separator);
}
static StringTokenizer setupClassPath() {
String classPath = System.getProperty("java.class.path");
String separator = System.getProperty("path.separator");
return new StringTokenizer(classPath, separator);
}
}