0

I am working on a webapp using Maven and Java servlets. I'm trying to build a connection to my local Postgres Database by retrieving its credentials from a Properties file because I don't want to hard code them. For this im using FileInputStream in a "getProperties" general method. The problem is that i am using a Tomcat Server, so the project runs from the Tomcat directory, and I'm having trouble setting it so that my fellow programmers can run my app on their own machines without having to hardcode a directory. The final goal is to only have to change the file itself and never the code.

So i have a Properties file like this:

port = localhost
bdname = clothingdb
user = jmbs
pw = 123

and im trying to retrieve them like this:

    String port = getProperties("port");
    String bdname = getProperties("bdname");
    String user = getProperties("user");
    String pw = getProperties("pw");

Using this method:

public String getProperties(String propriedade)
{
        String parametro = null;

        try (InputStream input = new FileInputStream("svlocal.properties")) {

            Properties prop = new Properties();

            // load a properties file
            prop.load(input);

            // get the property value and print it out
            parametro = prop.getProperty(propriedade);


        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return parametro;
    }

For this i get the "File not found" error, because the project runs from the tomcat/bin directory and not from the NetBeansProjects/bookstore/ directory where i need to have the Properties file.

My actual question is, how can i set the Properties file, so that when i send my project to someone else he only has to run it and change the credentials on the Properties file to access his own local database, and not having to change directories or hard code them on the getProperties() method? Thanks in advance.

2 Answers2

1

You can add the file in classpath and then load it as below,

ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream input = classLoader.getResourceAsStream("src/main/resources/svlocal.properties")

0

Ok, so, i was able to solve the problem using javaadict's suggestion but with a twist. I created a new directory in src/main/resources and added the properties file to it. then i used the following method:

public String getProperties(String propriedade){

        String parametro = null;
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        try (InputStream input = classLoader.getResourceAsStream("svlocal.properties")) {

            Properties prop = new Properties();

            // load a properties file
            prop.load(input);

            // get the property value and print it out
            parametro = prop.getProperty(propriedade);


        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return parametro;
    }

It seems that because im using maven it recognizes the resources folder in src/main/resources and i was able to load the file just by using "svlocal.properties" in the getResourceAsSteam method.

Ty for the valuable suggestion javaaddict.