以压缩文件或目录的方式过滤.class文件

解决方案 »

  1.   

    不光是jar中的类,只要当前classpath下同一包下的都要取得这些类
      

  2.   

    对于classes下的可以采用文件操作读取classes目录某个文件夹下的所有类,如果是JAR我就不太清楚如何拿出来了
      

  3.   

    能否说的详细一点吗?
    manifest是什么东西,在什么位置的
      

  4.   

    举个例子来说吧,我要找到 org.apache.test 这个包下的所有类,你怎么知道这些类是在什么jar中,有几个jar你知道吗?可能还有一些是在classes下面的类路径的,我现在要取的是只要附合这个org.apache.test 这个包下的类都要取出来.光一个manifest行吗?你去找那一个jar下面的manifest,你知道有几个jar吗?
      

  5.   

    除了用文件操作进行搜索之外,想不出其他方法。
    如果是jar文件,就先解压缩。
      

  6.   

    分析jar包既可, 每个jar文件和路径都能够给读出, 如果是.class 结尾的表示类
    public static void listZipClass(File file) throws Exception {
      ZipInputStream in = new ZipInputStream(new FileInputStream(file));
      try {
        ZipEntry ze = null;
        while ((ze = in.getNextEntry()) != null) {
          if (ze.isDirectory()) {
            continue;
          } else {
            String name = ze.getName();
            if (!name.endsWith(".class"))
              continue;
            name = name.replace('/', '.').replaceAll(".class", "");
            System.out.println(name);
          }
        }
      } finally {
        in.close();
      }
    }
      

  7.   

    public static void main(String[] args) {
    sun.tools.jar.Main jartool =
    new sun.tools.jar.Main(System.out, System.err, "jar");
    jartool.run(new String[]{"-tf","D:/Program Files/AdventNet/CLI/jars/comm.jar"});
    }
      

  8.   

    写了一个不是很高效的程序楼主运行测试一下,把org.apache.struts.action 换成你要的找的包名import java.io.File;
    import java.io.FileInputStream;
    import java.io.FilenameFilter;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;/**
     * 
     * @author ronny_zeng
     * 
     */
    public class SearchClass {
    public static void main(String[] args) throws Exception {
    String classpath = System.getProperty("java.class.path");
    String[] all = classpath.split(";");


    for(int i = 0;i<all.length;i++) {
    if(all[i].endsWith(".jar")) {
    findJar(all[i],"org.apache.struts.action");
    } else {
    findFile(all[i],"org.apache.struts.action");
    }

    }
    } /**
     * 
     * @param path
     * @param packName
     * @throws Exception
     */
    public static void findFile(String path, String packName) throws Exception {
    File file = new File(path + "\\" + packName.replace('.', '\\'));

    System.out.println(file.getAbsolutePath());
    if (file.exists() && file.isDirectory()) { File[] classes = file.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
    if (name.endsWith(".class")) {
    System.out.println(name);
    return true;
    }
    return false;
    }
    });
    System.out.println(classes.length);
    }
    } /**
     * 
     */
    public static void findJar(String jar ,String packName) throws Exception {
    ZipInputStream in = new ZipInputStream(new FileInputStream(
    new File(jar)));
    ZipEntry ze = null; while ((ze = in.getNextEntry()) != null) {
    if(ze.getName().startsWith(packName.replace(".","/"))&&ze.getName().endsWith(".class")&&(ze.getName().lastIndexOf("/")==(packName.replace(".","/")+"/").lastIndexOf("/"))) {
    System.out.println(ze.getName());
    }
    }
    }}
      

  9.   

    向interpb朋友表示忠心的感谢! 试了一下,是我需要的东西!