P A S T R Y
P A S T R Y

Reputation: 351

Tomcat deployment file paths (for Java I/O process)

I managed to implement a simple file I/O in my Spring project for files not inside my project (then WAR file) using a .properties file that contains the path (source) and now I need to do it in the server.

Before, when I included the files inside the WAR file, I can access them in the Tomcat server using the path

/home/my_username/tomcat7/webapps/my_project/WEB-INF/classes/

But now that the files are not inside the WAR file, I just put them adjacent to the webapps folder.

The structure of my Tomcat 7 folder is:

tomcat7
|----> bin
|----> conf
|----> logs
|----> resources*
|----> temp
|----> webapps
|----\----> springapp.war*
|----> work

where /resources is the folder I need inside the WAR file I/O.

My .properties file has:

dir.server=home/my_username/tomcat7/resources
// or
dir.server=/home/my_username/tomcat7/resources

but an error shows (from my catalina.out file):

java.io.FileNotFoundException: /home/my_username/tomcat7/resources/my_file.file (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:120)
    at java.io.FileInputStream.<init>(FileInputStream.java:79)
    at weka.core.SerializationHelper.read(SerializationHelper.java:270)

Is there a proper way to declare the file path or do I have to change my implementation of file I/O?

Upvotes: 0

Views: 646

Answers (1)

user2339071
user2339071

Reputation: 4310

The reason you are getting a FileNotFoundException is because your deployed war must be on another system or something like that that that doesn't contain the same exact path. You should use this line of code in your program.

String tomcatPath = System.getProperty("catalina.base")

This will return you the path to the tomcat folder in that particular system. Hence if system on which you deployed the war has a different path for tomcat, that will be returned. You can then access the properties file in the resources folder by appending the string to the path.

String tomcatPath = System.getProperty("catalina.base");
String propertiesFilePath = tomcatPath + "/resources";

To access a particular file you can use the code:

String filePath = propertiesFilePath+"my_file.file";

You should take a look at :

How to get Tomcat installation directory using Java.

Hope it helps.. :)

Upvotes: 2

Related Questions