0

I have a servlet and inside the WEB-INF/lib directory I have a jar file. This jar is made from a project I am working on. The code in the project executes fine and uses txt files in its main directory. Java code calling these files don't need to add any path, IE: new FileReader("file.txt").

The problem is when I call the servlet it keeps throwing exceptions it can't find those txt files required by the jar. I have placed the txt files in the following locations so far with no luck:

  • Main Directory
  • Source Folder
  • Servlet Package
  • WebContent
  • WEB-INF
  • WEB-INF/lib
  • META-INF

I think I've exhausted all the possible places the txt files could go. Where on earth is the jar file looking for them?

Thank you

user649716
  • 109
  • 13

3 Answers3

1

Take a look at this post.

It works when you're executing it as a project because that relative path is actually valid on the filesystem. However, once you've packaged your application as a JAR, the usual methods of reading a file will fail because your files are inside the JAR which to the filesystem is just another file. To read resources packaged inside your JAR, you ought to use the Class#getResource() or Class#getResourceAsStream() as appropriate.

You can also checkout what the Java Glosarry on mindprod.com has to say about these:

no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51
0

I think I've exhausted all the possible places the txt files could go. Where on earth is the jar file looking for them?

No you didn't. Place the file in the /bin directory inside Tomcat distribution. This construct:

new FileReader("file.txt")

searches for a file in process current directory. After starting Tomcat it happens to be the binary directory that holds all the startup scripts (very unfortunate place).

That said, you should consider rewriting this file reading open and make the path configurable as in most environments reading (and certainly writing to) a /bin directory should be forbidden and is a very bad practice.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • I've tried the bin folder without any luck. I am running it all in Eclipse. Will this make any difference? – user649716 Mar 13 '11 at 20:26
  • Yes, it makes the difference. Run `System.out.println(new File(".").getAbsolutePath());` somewhere before trying to open the `file.txt` (it will probably point to some directory inside Eclipse or your project directory. These struggles should only suggest you that you shouldn't rely on default directory and externalize this file's path. – Tomasz Nurkiewicz Mar 13 '11 at 20:56
  • This worked - thank you :) I will try and get it to lookup in a different directory now (instead of inside the eclipse root). What is the best way of doing this? – user649716 Mar 13 '11 at 23:24
  • Glad to hear that, but still remember that this is a bad idea to store application files inside Tomcat/Eclipse binary directories. Anyway please accept the answer if it solves your problems, thanks! – Tomasz Nurkiewicz Mar 14 '11 at 07:04
0

I think the answer depends on how Tomcat is started, but often the default directory is the Tomcat root.

Try the following in your servlet:

ServletContext context = getServletConfig().getServletContext ();
String fullPath = context.getRealPath ("file.txt");
Gene Golovchinsky
  • 6,101
  • 7
  • 53
  • 81
  • The path this prints out is an obscure temp folder. I have tried placing the files inside this hidden folder but it still doesn't work. – user649716 Mar 13 '11 at 20:27