Java代码:Web应用程序缺少类路径,但桌面应用程序中没有问题 [英] Java Code: Missing classpath from webapp but no issues in desktop app
问题描述
我正在尝试使用以下代码读取资源文件夹(src / main / resources)下的属性文件名
I am trying to read the property filenames under my resources folder (src/main/resources) with the below code
final String classPath = System.getProperty("java.class.path", ".");
System.out.println(classPath);
以上代码在桌面应用程序中可完美运行,但在System.out.println以上的Web应用程序中给我这样的输出:
Above code is working perfectly file in a desktop application but in web application above System.out.println is giving me this output:
/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)
(missing \target\classes)
在桌面应用上运行相同代码时,我成功获取了 \target\classes,可以在其中看到属性文件。
While running the same code on desktop app I am successfully getting "\target\classes" where I can see the properties file.
通过类路径读取这些文件的名称应该做些什么改变?请帮忙。
What change should I do to read the names of those files via classpath? Please help.
推荐答案
最后,我通过Google Relection API解决了这个问题。但是我仍然愿意接受其他解决方案。
Finally I solved this by google relection api. But I am still open for other solutions.
添加依赖项:
<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代码:Web应用程序缺少类路径,但桌面应用程序中没有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!