简体   繁体   中英

Is there a way to load information from a properties file that is outside a project's directory when the project gets exported as a JAR?

I need to keep database credentials in a properties file that is located outside of my project's folder/structure/class paths (ie C:/Parent_name/config/config.properties). The project (C:/Parent_name/Tomcat/Project_name/) gets exported as a JAR and is used in other applications (C:/Tomcat/webapps/app_1/WEB-INF/lib/Project_name.jar) that are then deployed to a Tomcat server.

I've found I can't do a simple FileInputStream because the file would need to be a part of the class path and I need the file to be outside of the project so DBA's can easily update credentials when needed. getResourceAsSteam() returns a null pointer exception.

Here's two examples of code that don't achieve what I need:

FileInputSteam

private static Object readConfigFile()  {

    try (InputStream input = new FileInputStream("C:\\Parent_name\\config\\config.properties")) {

        Properties prop = new Properties();

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

        // get the property value and print it out
        System.out.println(prop.getProperty("db.url"));
        System.out.println(prop.getProperty("db.user"));
        System.out.println(prop.getProperty("db.password"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return null;
}

getResrouceAsSteam()

private static Object readConfigFile() {
        try {
            Class cls = Class.forName("SQLCredentials");

            // returns the ClassLoader object associated with this Class
            ClassLoader cLoader = cls.getClassLoader();
            InputStream input = cLoader.getResourceAsStream("C:/Parent_name/config/config.properties");

            Properties prop = new Properties();

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

            // get the property value and print it out
            System.out.println(prop.getProperty("db.url"));
            System.out.println(prop.getProperty("db.user"));
            System.out.println(prop.getProperty("db.password"));
        } catch (IOException | ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        return null;
    }

Yes there is a way to "Is there a way to load information from a properties file that is outside a project's directory when the project gets exported as a JAR?"

  1. The project being exported as a JAR has no bearing on the ability to read a regular file in the file system as VGR pointed out in his very helpful comment.

  2. My problem was the file not being found. The debug step I took to lead me to the correct answer was listing all files in a directory

     String[] pathnames;

     // Creates a new File instance by converting the given pathname string
     // into an abstract pathname
     File f = new File("C:\\Parent_name\\config");

     // Populates the array with names of files and directories
     pathnames = f.list();

     // For each pathname in the pathnames array
     for (String pathname : pathnames) {
         // Print the names of files and directories
         System.out.println(pathname);
     }

Here I discovered that config.properties was actually config.properties.txt.

  1. In order to create a proper config.properties file, I created a new file in eclipse, named it config.properties, then copy/pasted it into C://Parent_name//config. I used the following code to read the file and print it's values:
private static Object readConfigFile()  {

  
 try (InputStream input = Files.newInputStream(Paths.get("C:\\Parent_name\\config\\config.properties"))) {
    
     Properties prop = new Properties();

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

     // get the property value and print it out
     System.out.println(prop.getProperty("dev.url"));
     System.out.println(prop.getProperty("dev.user"));
     System.out.println(prop.getProperty("dev.password"));
 } catch (IOException ex) {
    ex.printStackTrace();
 }

    return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM