Synopsis
Given the following (abridged) code that lives server-side on a servlet (Tomcat is the container). This is a GWT application, though that should be irrelevant (I think).
ServletContext context = getServletContext();
String dataFilePath= context.getRealPath("/WEB-INF/dir/dataFile.txt");
File dataFile = new File(dataFilePath);
TestCaseGenerator testCaseGenerator = new TestCaseGenerator(dataFile);
testCaseGenerator.generateTestCase();
TestCaseGenerator is a class from a jar in the project’s war/WEB-INF/lib folder, that’s been added to the GWT project as an external library.
The Problem
When testCaseGenerator.generateTestCase() gets executed, it’s unable to use dataFile to create a new LineNumberReader(new FileReader(dataFile));, a FileNotFoundException gets thrown.
I’ve verified that the String value of dataFilePath is correct and contains the proper real path to the file on the server I need to read from, as well that dataFile isn’t null. I’ve also verified that TestCaseGenerator runs just fine when called from a command line, outside of this GWT application.
I’m not sure why TestCaseGenerator isn’t able to use the File object I pass it, considering I’m passing it the real file path of the file. I can come up with some alternative solutions to get around this issue, but now I’m genuinely curious why it isn’t able to find the file.
Thanks in advance for any insight.
Solved
I foolishly neglected to include the
dir/dataFile.txtin mybuild.xml‘swartarget, so in fact the file wasn’t being included in thewarpackage, and thus never placed within the Tomcat container.And I also misunderstood what
context.getRealPath("/WEB-INF/dir/dataFile.txt");actual returns;. From ServletContext javadocSo even though
getRealPathreturns a path value, it doesn’t necessarily mean the file is at that actual path.