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

*爱你&永不变心* 提交于 2019-12-24 09:08:23

问题


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);

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

(missing \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.


回答1:


Finally I solved this by google relection api. But I am still open for other solutions.

Add dependency:

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

Java code:

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


来源:https://stackoverflow.com/questions/48376728/java-code-missing-classpath-from-webapp-but-no-issues-in-desktop-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!