getting package names from classes
I have this following java program which returns certain classes from a directory. I want to find the package names for these classes..how can i do that?? any code will be helpfull.
import java.util.*;
import java.io.*;
public class CreateTestPackage
{
public void execute()
{
List<Class> findClasses(File directory) throws ClassNotFoundException
{
List<Class> classes = new ArrayList<Class>();
if (!directory.exists())
{
return classes;
}
开发者_如何学C File[] files = directory.listFiles(new FilenameFilter()
{
public boolean accept( File dir, String name )
{
return name.matches("test.*\\.class");
}
});
for (File file : files)
{
if (file.isDirectory())
{
assert !file.getName().contains(".");
classes.addAll(findClasses(file));
}
else if (file.getName().endsWith(".class"))
{
classes.add(Class.forName(file.getName().substring(0, file.getName().length() - 6)));
}
}
return classes;
}
}
}
for(Class c : classes) {
c.getPackage().getName();
}
精彩评论