我正在尝试使用以下代码读取资源文件夹(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”,在其中可以看到属性文件。
我应该做什么改变才能通过类路径读取这些文件的名称?请帮忙。
最佳答案
最后我通过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/