how to get relative path of current directory in tomcat from linux environment using java

Your problem is that you don't know what the "current" directory is. If you start Tomcat on Linux as a service, the current directory could be anything. So new File(".") will get you a random place in the file system.

Using the system property catalina.base is much better because Tomcat's start script catalina.sh will set this property. So this will work as long as you don't try to run your app on a different server.

Good code would look like this:

File catalinaBase = new File( System.getProperty( "catalina.base" ) ).getAbsoluteFile();
File propertyFile = new File( catalinaBase, "webapps/strsproperties/strs.properties" );

InputStream inputStream = new FileInputStream( propertyFile );

As you can see, the code doesn't mix strings and files. A file is a file and a string is just text. Avoid using text for code.

Next, I'm using getAbsoluteFile() to make sure I get a useful path in exceptions.

Also make sure your code doesn't swallow exceptions. If you could have seen the error message in your code, you would have seen instantly that the code tried to look in the wrong place.

Lastly, this approach is brittle. Your app breaks if the path changes, when a different web server is used and for many other cases.

Consider extending the webapp strsproperties to accept HTTP requests. That way, you could configure your app to connect to strsproperties via an URL. This would work for any web server, you could even put the properties on a different host.


If your code runs inside a servlet, it may be simpler to use getServletContext().getRealPath("/strs.properties") to retrieve the absolute file path.