java 查找 Web 应用程序的类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6572415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:26:01  来源:igfitidea点击:

Finding the classpath of a web app

javaweb-applicationsclasspath

提问by Zemzela

When I'm working on my web app in Eclipse, I can search all the classes and JAR files with

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

String cp = System.getProperty("java.class.path");

but when I deploy on Tomcat or Jetty, I get nothing.

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

I want to find all *.propertiesfiles which are located in the webapp (either in the JAR or, if exploded, in folders).

我想找到*.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);
        }
}

This way, running the web application in Eclipse gives me all JAR files and all class folders, but not when I deploy the application in Tomcat.

这样,在 Eclipse 中运行 Web 应用程序会给我所有 JAR 文件和所有类文件夹,但当我在 Tomcat 中部署应用程序时不会。

It would be enough if I could just get the path to WEB_INF. From there, it's easy.

如果我能找到通往WEB_INF. 从那里,这很容易。

回答by Zemzela

If someone needs the answer, I found a very obvious answer.

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

getServletContext().getRealPath("/WEB-INF")

Then when you receive the real path you can easily search through the folders and JARs.

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