在windows文件夹中快速查找java类

在windows文件夹中快速查找java类,java,Java,给定类名:com.example.MyClass(或者改为“/”),如何快速确定包含一堆JAR/class文件的文件夹中是否存在java类 目前我唯一的解决方案是使用归档检查器(7zip)打开它们并搜索目录 任何比这更好的都会很有帮助 编辑:此外,我正在寻找比在IDE中创建新项目、添加文件夹并使用其工具查找类更快的方法 更新:如果这个问题没有得到有用的答案,我很抱歉,在README.md中有一个“评论问题”部分,人们可以帮助我回答构建实用程序所需的一些问题。此外,欢迎您提出意见和建议的实施方式

给定类名:
com.example.MyClass
(或者改为“
/
”),如何快速确定包含一堆JAR/class文件的文件夹中是否存在java类

目前我唯一的解决方案是使用归档检查器(7zip)打开它们并搜索目录

任何比这更好的都会很有帮助

编辑:此外,我正在寻找比在IDE中创建新项目、添加文件夹并使用其工具查找类更快的方法

更新:如果这个问题没有得到有用的答案,我很抱歉,在README.md中有一个“评论问题”部分,人们可以帮助我回答构建实用程序所需的一些问题。此外,欢迎您提出意见和建议的实施方式


更新:user3819021的解决方案很有帮助,尽管它取决于cygwin的存在,但仍然有效,如果有人知道仅限windows的解决方案,我们将不胜感激。

打开powershell提示符并运行

gci -LiteralPath "C:\work\src\javastuff" -Filter *.class | ? { $_.FullName -match "my.package.SomeClass.class" }
-如果您只需要完整路径和文件名列表,请附加此位:

| % { $_.FullName }
如果已在计算机上安装,则可以尝试以下代码:

find <your root path> -name '*.jar' | while read file; do unzip -l "$file" | grep -q <search file> && echo $file; done
find-name'*.jar'|读取文件时;解压缩-l“$file”| grep-q&&echo$file;完成

应该类似于
/path/YourClass.class
或只是
YourClass.class

以下代码将在整个类路径中搜索特定类。在没有参数的情况下,它将转储它找到的每个类,然后您可以通过管道连接到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);
  }
}
导入java.io.File;
导入java.io.IOException;
导入java.util.Enumeration;
导入java.util.StringTokenizer;
导入java.util.Vector;
导入java.util.zip.ZipEntry;
导入java.util.zip.ZipFile;
公共期末班{
私有WhichClass(){
}
静态矢量;
公共静态void main(最终字符串[]argv){
向量v;
如果((argv.length==0)| |“-all”.equals(argv[0])){
v=findClass(空);
}否则{
v=findClass(argv[0]);
}
对于(int i=0;i
听起来像是市场上的一个空白,如果有一个小型Java应用程序,它会很有用。good call@Stewart,我在考虑菜单上下文项,你怎么看?我已经做了多少次了