查找Web应用程序的类路径

Zem*_*ela 4 java web-applications classpath

当我在Eclipse中使用我的Web应用程序时,我可以搜索所有类和JAR文件

String cp = System.getProperty("java.class.path");
Run Code Online (Sandbox Code Playgroud)

但是当我在Tomcat或Jetty上部署时,我什么也得不到.

我想找到*.properties位于webapp中的所有文件(在JAR中,如果是爆炸的,在文件夹中).

String cp = System.getProperty("java.class.path");
String pathSep = File.pathSeparator;
String[] jarOrDirectories = cp.split(pathSep);
for (String fileName : jarOrDirectories) {
        File file = new File(fileName);
        if (file.isFile()) {
                logger.debug("From jar "+file.getName());
                loadFromJar(file.getPath());
        } else {
                logger.debug("From folder "+file.getName());
                listFolder(file);
        }
}
Run Code Online (Sandbox Code Playgroud)

这样,在Eclipse中运行Web应用程序会为我提供所有JAR文件和所有类文件夹,但在Tomcat中部署应用程序时则不行.

如果我能够获得通路就足够了WEB_INF.从那里,这很容易.

Zem*_*ela 8

如果有人需要答案,我找到了一个非常明显的答案.

getServletContext().getRealPath("/WEB-INF")
Run Code Online (Sandbox Code Playgroud)

然后,当您收到真实路径时,您可以轻松搜索文件夹和JAR.