Java代码: Missing classpath from webapp but no issues in desktop app

标签 java web-applications classpath

我正在尝试使用以下代码读取资源文件夹(src/main/resources)下的属性文件名

final String classPath = System.getProperty("java.class.path", ".");
System.out.println(classPath);

上面的代码在桌面应用程序中完美运行,但在上面的 Web 应用程序中 System.out.println 给出了以下输出:

<小时/>
/C:/Development/INSTALLED/apache-tomcat-8.0.46/bin/bootstrap.jar
/C:/Development/INSTALLED/apache-tomcat-8.0.46/bin/tomcat-juli.jar
/C:/Program%20Files/RedHat/java-1.8.0-openjdk-1.8.0.151-1/lib/tools.jar

(缺少\目标\类)

在桌面应用程序上运行相同的代码时,我成功获取“\target\classes”,在其中可以看到属性文件。

我应该做什么改变才能通过类路径读取这些文件的名称?请帮忙。

enter image description here

最佳答案

最后我通过google reelection api解决了这个问题。但我仍然愿意接受其他解决方案。

添加依赖项:

<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.9.11</version>
</dependency>

Java代码:

Reflections reflections = new Reflections("translation", new ResourcesScanner());
final Set<String> fileNames = reflections.getResources(pattern);

关于Java代码: Missing classpath from webapp but no issues in desktop app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48376728/

相关文章:

java - 匹配列表中的字符串并提取值

java - Spring Boot @RestController,在 @RequestBody 中反序列化 Collection 时容忍 MismatchedInputException

java - 将 Java Web 应用程序 (Struts) 置于维护模式的理想方法是什么?

Glassfish 4.1 - 最新 Jersey 版本

java - 在 ant 中定义 Main-Class 用于捆绑 jar 的混淆

java - 如何在android中获取Facebook信息

java - 一种使用 Selenium 定位元素的方法

java - Dart 中的图像处理和存储

html - 使用内联 block 有什么问题?

java - Tomcat-servlet-api.jar 是否加载到类路径?