yang3wei的专栏

Enjoy my life!

Java Get classes In Package

| Comments

直接上代码:

GetClassesInPackage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package org.bruce.pocket.tools.utils;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

/**
 * @author Bruce Yang
 * http://www.roseindia.net/java/java-get-example/get-classes-package.shtml
 */
public class GetClassesInPackage {
  private static boolean getJar = true;

  public static List<String> getClasseNamesInPackage(String jarName, String packageName) {
      if (getJar) {
          System.out.println("Jar \"" + jarName + "\" for \"" + packageName + '\"');
      }
      
      ArrayList<String> arrayList = new ArrayList<String>();
      // replaceAll 第一个参数是正则字符串,第二个参数是普通的替换字符串,不需要对正则进行转义~
      packageName = packageName.replaceAll("\\.", "/");
      
      try {
          JarInputStream jarFile = new JarInputStream(new FileInputStream(jarName));
          JarEntry jarEntry;
          
          while (true) {
              jarEntry = jarFile.getNextJarEntry();
              if (jarEntry == null) {
                  break;
              }
              if ((jarEntry.getName().startsWith(packageName))
                      && (jarEntry.getName().endsWith(".class"))) {
                  arrayList.add(jarEntry.getName().replaceAll("/", "."));
              }
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
      return arrayList;
  }

  public static void main(String[] args) {
      String jarPath = "/Users/user/Desktop/JavaDynamicComiple.jar";
      String pkgPath = "org.bruce.pocket.tools.items";
      List<String> list = GetClassesInPackage.getClasseNamesInPackage(jarPath, pkgPath);
      System.out.println("Found: ");
      for (String item : list) {
          System.out.println(item);
      }
  }
}

Comments