问题
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