4

I have a simple web application which uses Apache Tomcat 6.0. I am trying to read properties file from the path "resources/mysql.properties". Here "resources" folder is outside the "src" folder. When I am trying to run the project as Java Application it works fine. But It throws FileNotFoundException when I run it on the Server.

This is the Java code with main, which I am running on Console.

package com.jm.test;

import com.jm.util.PropertyUtil;

public class CodeTester {

    public static void main(String[] args) {
            System.out.println(PropertyUtil.getDBPropertyValue("driver"));
    }

}

This is the code of PropertyUtil.Java file.

    /**
     * 
     */
    package com.jm.util;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.MissingResourceException;
    import java.util.Properties;

    import org.apache.log4j.Logger;

    /**
     * @author Jaydeep Ranipa
     *
     */
    public class PropertyUtil {
        public static final Logger log = Logger.getLogger(PropertyUtil.class);
        private static final String resourceDir = "resources";

        private PropertyUtil() {
        }

        private static Properties loadProperties(String fileName) throws IOException, FileNotFoundException {
            System.out.println("filename: "+fileName);
            InputStream fileStream = new FileInputStream(new File(fileName));
            Properties props = new Properties();
            props.load(fileStream);
            return props;
        }

        public static String getDBPropertyValue(String key) {
            Properties prop = null;
            String value = "";
            String fileName = "localmysql.properties";

            try {
                prop = loadProperties(resourceDir + "/" + fileName);
                value = prop.getProperty(key);
                if (value == null) {
                    throw new MissingResourceException("Property not found.", "DATABASE", key);
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                System.out.println("properties file not found");
                e.printStackTrace();
                log.error("Properties file <<<"+resourceDir + "/" + fileName +">>> not found.");
                value = "Error occured.";
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("properties file reading failed");
        log.error("Properties file <<<"+resourceDir + "/" + fileName +">>> reading failed.");
                value = "Error occured.";
            } catch (MissingResourceException e) {
                // TODO: handle exception
                System.out.println("property not found");
                log.error("Property <<<"+e.getKey()+">>> for <<<"+e.getClassName()+">>> not found.");
                value = "Error occured.";
            }
            return value;
        }

        public static String getErrorMessage(String errorCode) {
            Properties prop = null;
            String message = "";
            String fileName = "errormessage.properties";

            try {
                prop = loadProperties(resourceDir + "/" + fileName);
                message = prop.getProperty(errorCode);
                if (message == null) {
                    throw new MissingResourceException("Property not found.", "ERROR", errorCode)
                }
            } catch (FileNotFoundException e) 
                // TODO Auto-generated catch block
                log.error("Properties file <<<"+resourceDir + "/" + fileName +">>> not found.")
                message = "Something went wrong.";
            } catch (IOException e) {
                // TODO Auto-generated catch bloc
                log.error("Properties file <<<"+resourceDir + "/" + fileName +">>> reading failed.");
                message = "Something went wrong.";
            } catch (MissingResourceException e) {
                // TODO: handle exception
                log.error("Property <<<"+e.getKey()+">>> for <<<"+e.getClassName()+">>> not found.");
                message = "Something went wrong.";
            }
            return message
        }
    }

The following code gives the error when executing through web server.

    Class.forName(PropertyUtil.getDBPropertyValue("driver"));
    con =  DriverManager.getConnection(PropertyUtil.getDBPropertyValue("url"), 
    PropertyUtil.getDBPropertyValue("username"), 
    PropertyUtil.getDBPropertyValue("password"));
    return con;

Here is the project structure.Project Structure

Jaydeep Ranipa
  • 421
  • 5
  • 16
  • You should be loading your properties with [Class.getResourceAsStream](http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResourceAsStream-java.lang.String-). An entry in a .jar or .war is not an actual file, so you cannot load it with FileInputStream. – VGR Dec 05 '16 at 16:08
  • try invoking filename directly without the resourceDir and see if that helps. You can also try escaping the slash and try. – yogidilip Dec 05 '16 at 16:09

2 Answers2

3

How to find the working folder of a servlet based application in order to load resources

Check out this thread and instead of:

InputStream fileStream = new FileInputStream(new File(fileName));

try:

InputStream fileStream = getServletContext().getResourceAsStream(fileName);
Community
  • 1
  • 1
Marcin Zareba
  • 430
  • 7
  • 18
  • How do I get absolute path for all the properties file listed above in resources folder? I want to access them in console as well as web application. – Jaydeep Ranipa Dec 05 '16 at 16:42
0

In tomcat sub directory "/lib" add the driverDB.

  • This might work, but not a good advice. `bin` directory of Tomcat should not be used for app-specific resources. – ujulu Dec 05 '16 at 16:30