Bishoy Bishoy - 1 year ago 161
Java Question

FileNotFoundException when accessing a resource file after deploy to tomcat

I got two maven projects:

  1. A JAR with a resource file accessed from its code

  2. A WAR that has a RESTful service that uses the JAR

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


File file = new File(getClass().getResource("/file.xml").getFile());

When debugging, I found the value of
is as following:


I've opened the JAR at that location and found the file there. So any idea why this is happening.

Answer Source

Try to use getResourceAsStream() insteed 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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download