Your problem is that you do not know what the "current" directory is. If you are running Tomcat on Linux as a service, the current directory can be anything. Thus, new File(".") Will give you a random place in the file system.
Using the system property catalina.base much better, because Tomcat start script catalina.sh set this property. That way, this will work until you try to run the application on another 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 does not mix lines and files. A file is a file, and a line is just text. Avoid using text for code.
Then I use getAbsoluteFile() to make sure I get the useful path in the exceptions.
Also make sure your code does not swallow exceptions. If you could see the error message in your code, you would immediately see what the code tried to find in the wrong place.
Finally, this approach is fragile. Your application crashes if the path changes when another web server is used and for many other cases.
Consider the webapp extension strsproperties to accept HTTP requests. Thus, you can configure the application to connect to strsproperties through the URL. This will work on any web server, you can even put it on another host.
source share