packageorg.bruce.pocket.tools.utils;importjava.io.FileInputStream;importjava.util.ArrayList;importjava.util.List;importjava.util.jar.JarEntry;importjava.util.jar.JarInputStream;/** * @author Bruce Yang * http://www.roseindia.net/java/java-get-example/get-classes-package.shtml */publicclassGetClassesInPackage{privatestaticbooleangetJar=true;publicstaticList<String>getClasseNamesInPackage(StringjarName,StringpackageName){if(getJar){System.out.println("Jar \""+jarName+"\" for \""+packageName+'\"');}ArrayList<String>arrayList=newArrayList<String>();// replaceAll 第一个参数是正则字符串,第二个参数是普通的替换字符串,不需要对正则进行转义~packageName=packageName.replaceAll("\\.","/");try{JarInputStreamjarFile=newJarInputStream(newFileInputStream(jarName));JarEntryjarEntry;while(true){jarEntry=jarFile.getNextJarEntry();if(jarEntry==null){break;}if((jarEntry.getName().startsWith(packageName))&&(jarEntry.getName().endsWith(".class"))){arrayList.add(jarEntry.getName().replaceAll("/","."));}}}catch(Exceptione){e.printStackTrace();}returnarrayList;}publicstaticvoidmain(String[]args){StringjarPath="/Users/user/Desktop/JavaDynamicComiple.jar";StringpkgPath="org.bruce.pocket.tools.items";List<String>list=GetClassesInPackage.getClasseNamesInPackage(jarPath,pkgPath);System.out.println("Found: ");for(Stringitem:list){System.out.println(item);}}}