Java getResources()返回仅包含1个对象的枚举
我尝试从类路径(web inf/classes/dir)获取文件列表 枚举en=getClass().getClassLoader().getResources(“目录”);Java getResources()返回仅包含1个对象的枚举,java,jakarta-ee,resources,classpath,Java,Jakarta Ee,Resources,Classpath,我尝试从类路径(web inf/classes/dir)获取文件列表 枚举en=getClass().getClassLoader().getResources(“目录”); 但是,唯一的元素不是此文件夹中的元素,而是文件夹本身。是否有一种方法可以获取文件列表,或者唯一的方法是逐个访问文件。因为当我尝试引用同一文件夹中的某个文件时,我可以轻松访问其内容。您不能递归浏览类路径上的目录 见此帖: 但是,如果您知道您正在查看一个JAR文件,您可以直接将其作为归档文件打开并浏览这些文件 有人对这个问题
但是,唯一的元素不是此文件夹中的元素,而是文件夹本身。是否有一种方法可以获取文件列表,或者唯一的方法是逐个访问文件。因为当我尝试引用同一文件夹中的某个文件时,我可以轻松访问其内容。您不能递归浏览类路径上的目录 见此帖: 但是,如果您知道您正在查看一个JAR文件,您可以直接将其作为归档文件打开并浏览这些文件 有人对这个问题给出了一个有用的答案: 您可以修改此代码以浏览类路径上的所有JAR文件和目录,并自己为目录名应用一些过滤器。该示例将列出gnu.trove.*包中的所有类:
import java.io.File;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Test
{
public static void main(String[] args)
{
final String dirname = "gnu.trove.";
findClasses(new Visitor<String>() {
@Override
public boolean visit(String classname)
{
if (classname.startsWith(dirname)) {
System.out.println(classname);
}
return true;
}
});
}
public interface Visitor<T>
{
public boolean visit(T t);
}
public static void findClasses(Visitor<String> visitor)
{
String classpath = System.getProperty("java.class.path");
String[] paths = classpath.split(System.getProperty("path.separator"));
String javaHome = System.getProperty("java.home");
File file = new File(javaHome + File.separator + "lib");
if (file.exists()) {
findClasses(file, file, true, visitor);
}
for (String path : paths) {
file = new File(path);
if (file.exists()) {
findClasses(file, file, true, visitor);
}
}
}
private static boolean findClasses(File root, File file,
boolean includeJars, Visitor<String> visitor)
{
if (file.isDirectory()) {
for (File child : file.listFiles()) {
if (!findClasses(root, child, includeJars, visitor)) {
return false;
}
}
} else {
if (file.getName().toLowerCase().endsWith(".jar") && includeJars) {
JarFile jar = null;
try {
jar = new JarFile(file);
} catch (Exception ex) {
}
if (jar != null) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
int extIndex = name.lastIndexOf(".class");
if (extIndex > 0) {
if (!visitor.visit(name.substring(0, extIndex)
.replace("/", "."))) {
return false;
}
}
}
}
} else if (file.getName().toLowerCase().endsWith(".class")) {
if (!visitor.visit(createClassName(root, file))) {
return false;
}
}
}
return true;
}
private static String createClassName(File root, File file)
{
StringBuffer sb = new StringBuffer();
String fileName = file.getName();
sb.append(fileName.substring(0, fileName.lastIndexOf(".class")));
file = file.getParentFile();
while (file != null && !file.equals(root)) {
sb.insert(0, '.').insert(0, file.getName());
file = file.getParentFile();
}
return sb.toString();
}
}
导入java.io.File;
导入java.util.Enumeration;
导入java.util.jar.JarEntry;
导入java.util.jar.jar文件;
公开课考试
{
公共静态void main(字符串[]args)
{
最后一个字符串dirname=“gnu.trove。”;
FindClass(新访问者(){
@凌驾
公共布尔访问(字符串类名称)
{
if(classname.startsWith(dirname)){
System.out.println(类名);
}
返回true;
}
});
}
公共界面访问者
{
公众参观(T);
}
公共静态void findClass(访问者)
{
字符串classpath=System.getProperty(“java.class.path”);
String[]path=classpath.split(System.getProperty(“path.separator”);
字符串javaHome=System.getProperty(“java.home”);
File File=新文件(javaHome+File.separator+“lib”);
if(file.exists()){
FindClass(文件、文件、true、访问者);
}
用于(字符串路径:路径){
文件=新文件(路径);
if(file.exists()){
FindClass(文件、文件、true、访问者);
}
}
}
私有静态布尔FindClass(文件根、文件名、,
布尔值(包括JARS、访客)
{
if(file.isDirectory()){
对于(文件子项:File.listFiles()){
if(!findClasses(根、子、includeJars、访问者)){
返回false;
}
}
}否则{
if(file.getName().toLowerCase().endsWith(“.jar”)&&includeJars){
JarFile jar=null;
试一试{
jar=新的JarFile(文件);
}捕获(例外情况除外){
}
if(jar!=null){
枚举条目=jar.entries();
while(entries.hasMoreElements()){
JarEntry=entries.nextElement();
String name=entry.getName();
int dex=name.lastIndexOf(“.class”);
如果(指数>0){
if(!visitor.visit)(name.substring(0,dex)
.替换(“/”,“)){
返回false;
}
}
}
}
}else if(file.getName().toLowerCase().endsWith(“.class”)){
如果(!visitor.visit(createClassName(根,文件))){
返回false;
}
}
}
返回true;
}
私有静态字符串createClassName(文件根,文件名)
{
StringBuffer sb=新的StringBuffer();
字符串文件名=file.getName();
sb.append(fileName.substring(0,fileName.lastIndexOf(“.class”));
file=file.getParentFile();
while(file!=null&&!file.equals(root)){
sb.insert(0,'.')。insert(0,file.getName());
file=file.getParentFile();
}
使某人返回字符串();
}
}
下面是一些代码,用于忽略JAR文件,只查看类路径上目录上的文件:
import java.io.File;
public class Test
{
public static void main(String[] args)
{
findClasses(new Visitor<String>() {
@Override
public boolean visit(String classname)
{
// apply your filtering here
System.out.println(classname);
return true;
}
});
}
public interface Visitor<T>
{
public boolean visit(T t);
}
public static void findClasses(Visitor<String> visitor)
{
String classpath = System.getProperty("java.class.path");
String[] paths = classpath.split(System.getProperty("path.separator"));
for (String path : paths) {
File file = new File(path);
// Ignore JAR files, just go through directories on the classpath
if (file.isFile()) {
continue;
}
findFiles(file, file, visitor);
}
}
private static boolean findFiles(File root, File file,
Visitor<String> visitor)
{
if (file.isDirectory()) {
for (File child : file.listFiles()) {
if (!findFiles(root, child, visitor)) {
return false;
}
}
} else {
if (!visitor.visit(createRelativePath(root, file))) {
return false;
}
}
return true;
}
private static String createRelativePath(File root, File file)
{
StringBuffer sb = new StringBuffer();
sb.append(file.getName());
file = file.getParentFile();
while (file != null && !file.equals(root)) {
sb.insert(0, '/').insert(0, file.getName());
file = file.getParentFile();
}
return sb.toString();
}
}
导入java.io.File;
公开课考试
{
公共静态void main(字符串[]args)
{
FindClass(新访问者(){
@凌驾
公共布尔访问(字符串类名称)
{
//在这里应用你的过滤
System.out.println(类名);
返回true;
}
});
}
公共界面访问者
{
公众参观(T);
}
公共静态void findClass(访问者)
{
字符串classpath=System.getProperty(“java.class.path”);
String[]path=classpath.split(System.getProperty(“path.separator”);
用于(字符串路径:路径){
文件=新文件(路径);
//忽略JAR文件,只需遍历类路径上的目录
if(file.isFile()){
继续;
}
FindFile(文件、文件、访问者);
}
}
私有静态布尔FindFile(文件根、文件名、,
访客(访客)
{
if(file.isDirectory()){
对于(文件子项:File.listFiles()){
if(!findFiles(根、子、访问者)){
返回false;
}
}
}否则{
如果(!visitor.visit(createRelativePath(根,文件))){
返回false;
}
}
返回true;
}
私有静态字符串createRelativePath(文件根,文件名)
{
StringBuffer sb=新的StringBuffer();
sb.append(file.getName());
file=file.getParentFile();
while(file!=null&&!file.equals(root)){
sb.insert(0,“/”).insert(0,file.getName());
import java.io.File;
public class Test
{
public static void main(String[] args)
{
findClasses(new Visitor<String>() {
@Override
public boolean visit(String classname)
{
// apply your filtering here
System.out.println(classname);
return true;
}
});
}
public interface Visitor<T>
{
public boolean visit(T t);
}
public static void findClasses(Visitor<String> visitor)
{
String classpath = System.getProperty("java.class.path");
String[] paths = classpath.split(System.getProperty("path.separator"));
for (String path : paths) {
File file = new File(path);
// Ignore JAR files, just go through directories on the classpath
if (file.isFile()) {
continue;
}
findFiles(file, file, visitor);
}
}
private static boolean findFiles(File root, File file,
Visitor<String> visitor)
{
if (file.isDirectory()) {
for (File child : file.listFiles()) {
if (!findFiles(root, child, visitor)) {
return false;
}
}
} else {
if (!visitor.visit(createRelativePath(root, file))) {
return false;
}
}
return true;
}
private static String createRelativePath(File root, File file)
{
StringBuffer sb = new StringBuffer();
sb.append(file.getName());
file = file.getParentFile();
while (file != null && !file.equals(root)) {
sb.insert(0, '/').insert(0, file.getName());
file = file.getParentFile();
}
return sb.toString();
}
}