0

I'm trying to use .properties files for the first time, in which I stored some configuration information for my web application in Java, I have a class and a method dedicated to this, however, every time it returns me an exception like "FileNotFoundException: Unable to find the specified path". Here it is the code:


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class ClassProperties {
    
    private static Properties property_file;
    
    public static String getConfProperty(String property){
        String value = null;
        try{
            FileReader reader = new FileReader("src/java/Utilities/config.properties");
            property_file = new Properties();
            property_file.load(reader);

            value = property_file.getProperty(property);  
        }
        catch(FileNotFoundException e){
            System.out.println(e);
        } 
        catch (IOException ex){
            System.out.println(ex);
        }
        return value;
    }
}

from the root folder of the project I specified the path to the files, what did I wrong?

Zoythrus
  • 165
  • 1
  • 11
  • 2
    This is a relative path (src/...) to the running JVM. I do not see anything specific why this is a web application. Anyway, first try to give the absolute path or look for the result or `System.getProperty("user.dir")` to get the current working directory of the JVM. – PeterMmm Oct 11 '22 at 11:48
  • Then come back with more specific info about your framework, Spring, plain servlet, etc. Most webapp frameworks has utilities for reading any configuration. – PeterMmm Oct 11 '22 at 11:52
  • You should load that [as a resource](https://technojeeves.com/index.php/aliasjava1/78-loading-files-as-resources-in-java-with-netbeans). Use try-with-resources as the loading *won't* close the stream – g00se Oct 11 '22 at 12:00
  • I made several attempts: first I tried entering the absolute path, and it works. Next, as suggested by @PeterMmm I ran ```System.getProperty("user.dir")``` and it returned the path to my web server, which is ```C:\Program Files\Apache Software Foundation\Apache Tomcat 8.0.27\bin,``` so I assume that my ```config.properties``` file should be placed in there, unless I specify the absolute path, am I right? I thought the root folder of the project was the same one used to search for classes (which is the one I used, in fact if I need to upload a javascript file, the path I used works). – Zoythrus Oct 11 '22 at 13:24
  • 1
    *so I assume that my config.properties file should be placed in there* Absolutely not. A properties file is a resource and should be treated as such. Web apps should only be reading the file system in special circumstances – g00se Oct 11 '22 at 13:26
  • 1
    Maybe this helps you: https://stackoverflow.com/questions/2161054/where-to-place-and-how-to-read-configuration-resource-files-in-servlet-based-app it depends on the config you want manage. – PeterMmm Oct 11 '22 at 13:51
  • @g00se exactly what I thought, it seemed strange to me, despite being the first time using these files. Anyway, I followed the thread suggested by @PeterMmm and it gave me cue to find a solution, so I simply modify the try like this: ```try (InputStream input = AppProperties.class.getClassLoader().getResourceAsStream("Utilities/config.properties")) {``` instead of the FireReader, I'm using an InputStream, treating it as a resource as you said, and now it works fine; path was also simplified, it was not necessary to specify folders before, as the root takes everything from the src java folder. – Zoythrus Oct 11 '22 at 15:46
  • All good. That can probably be simplified to ```try (InputStream input = getClass().getResourceAsStream("Utilities/config.properties")) {``` but I'd recommend you use root-relative resource paths as you can get caught out if you move the code to a different package. So "/Utilities/config.properties" if that first atom is at the root. Or you perhaps should call the path 'absolute' ;) – g00se Oct 11 '22 at 16:01

0 Answers0