Reputation: 725
I got two maven projects:
When the JAR unit tests run, it is able to access the resource file.
After deploying to tomcat, the access to the resource file fails with FileNotFoundException
I tried both these lines but the same results:
File file = new File(getClass().getClassLoader().getResource("file.xml").getFile());
And
File file = new File(getClass().getResource("/file.xml").getFile());
When debugging, I found the value of file
is as following:
file:/usr/share/tomcat8/webapps/ROOT/WEB-INF/lib/com.projectgroup.projectname-1.0-SNAPSHOT.jar!/file.xml
I've opened the JAR at that location and found the file there. So any idea why this is happening.
Upvotes: 0
Views: 1417
Reputation: 10843
Try to use getResourceAsStream()
instead of resource.getFile()
Another way to get your xml file loaded in your classpath is by using the Spring Framework as below:
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
resolver.getResources("classpath*:your/package/**/file.xml");
Upvotes: 1
Reputation: 609
Your file has to be in the classpath in the war file, is it being packed into the war as part of the build process?
Upvotes: 0