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

40,558

Solution 1

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.

Solution 2

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

Solution 3

This is not a robust way to go about it. If you want your webapp to consume data from 'outside', then call System.getProperty to get an absolute pathname that you, yourself, specify with -D in the tomcat config script.

Share:
40,558
asiam9
Author by

asiam9

Updated on September 09, 2020

Comments

  • asiam9
    asiam9 over 3 years

    I would like to use to read properties file from out side of the my web application. I was deployed a war file in tomcat in windows environment and I am able to read properties file from outside of my web application using following code.

    //(Method 1.)
    String filePath = 
    new java.io.File( ".").getCanonicalPath()+"/webapps/strsproperties/strs.properties";
    // or  
    //(Method 2.)
    String filePath = new File(System.getProperty("catalina.base"))+ "/webapps/strsproperties/strs.properties";
    
    InputStream inputStream = null;
    inputStream = new FileInputStream(filePath);
    properties.load(inputStream);
    String application = properties.getProperty("applications");
    

    In above both case's I am able to read filePath in windows.

    Problem is I am not able to read filePath in Linux using 1st method ( relative path) process.

    Is there any way to read file using relative path in tomcat from Linux env ?.

  • asiam9
    asiam9 over 11 years
    -- Works fine using class loader (with properties file within the application)
  • asiam9
    asiam9 over 11 years
    -- Works fine with properties file outside the application in some folder under Tomcat, if I use propertiesFilePath = new File(System.getProperty("catalina.base")) -- To check current directory in the applciation context I did urrentDirectory.getCanonicalPath(), and gave relative path to the properties file. The above works fine in Windows (accepting current path as, ex: D:/Tomcat/bin), however in Linux, it doesn't work taking current path as user's root, ex: '/' -- However the above works when I execute a standalone java program (below), it just doesn't work in Tomcat's context.
  • Christian Dechery
    Christian Dechery over 10 years
    This solution seems to work only in "regular" installations of Tomcat. I tried it on a Tomcat within an IDE (NetBeans in this case), and cataline.base points to a directory which only holds config and log files. The actual webapp is in another dir. :(