gpt4 book ai didi

java - 查找网络应用程序的类路径

转载 作者:搜寻专家 更新时间:2023-11-01 01:57:01 25 4
gpt4 key购买 nike

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

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

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

我想找到位于 web 应用程序中的所有 *.properties 文件(在 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);
}
}

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

如果我能得到WEB_INF的路径就足够了。从那里开始,这很容易。

最佳答案

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

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

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

关于java - 查找网络应用程序的类路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6572415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com