3

I have a properties file called admin.properties under src/config and whenever I run the program it gives me this error:

java.io.FileNotFoundException: admin.properties (The system cannot find the file specified)

Here is my code:

package com.in.props;

public class Props {
    public static void main(String[] args) {
        String filePath = "config/admin.properties";
        Properties adminProps = new Properties();
        adminProps.load(new FileInputStream(filePath));
        String userName = adminProps.getProperty("userName").trim();
        String password = adminProps.getProperty("password").trim();
    }
}

Here's the properties file, admin.properties:

userName=test
password=test

My Props class (in com.in.props) and the admin.properties (in config) are under different directories.

Project_Root
-src
 -config
   -admin.properties
 -com
   -in
     -props
       -Props.java

I am not using eclipse and I want to execute this via command prompt.

durron597
  • 31,968
  • 17
  • 99
  • 158
haja
  • 135
  • 1
  • 4
  • 12
  • 3
    The `admin.properties` are in you `src` directory, which means that once you jar the project, the file will be embedded within your jar and no longer accessible from the file system as a separate file. In this you'd have to use `Props.class.getResourceAsStream("config/admin.properties");`. Beware, in this configuration, the properties will be read only, you won't be able to write to the resource. If you need read/write access, the `admin.properties` file will need to be stored externally to the jar file – MadProgrammer Sep 02 '15 at 02:37
  • 1
    @MadProgrammer he never said he jarred anything up, but if he did, then that would be why. My guess is that he is running his program from a directory that is not src. So, when you specify the relative path 'config/admin.properties', that makes the bold assumption that you are executing the program from src. If you are not, then that is why this is happening. Generally why it's a better idea to figure out some better way to determine the path of that file, or use an absolute path. By jarring it like MadProgrammer says, you avoid this because you control where it goes in the jar when you jar it – searchengine27 Sep 02 '15 at 03:02
  • @searchengine27 Using a JAR file is a valid assumption. Mad if you don't actually. – user207421 Sep 02 '15 at 03:08
  • I didn't say it wasn't...I said that the OP never said that... – searchengine27 Sep 02 '15 at 03:09
  • @searchengine27 It's an assumption on my part, but if it's in the `src` directory, it's in a bad place (for been read as a file), but that comes down to project semantics and preferences ;) – MadProgrammer Sep 02 '15 at 03:11
  • I agree, all I was implying is that it looks like he's learning how to use a properties file, so we need to consider that maybe that properties file could be in any number of states or places – searchengine27 Sep 02 '15 at 03:12

1 Answers1

3

It's much easier to load files using the ClassLoader. Use getClass().getResourceAsStream() to get files that are on your classpath:

InputStream is = Props.class.getResourceAsStream("/src/config/admin.properties");
if(is != null) {
    Properties adminProps = new Properties();
    adminProps.load(is);

Note that the leading slash is very important.

durron597
  • 31,968
  • 17
  • 99
  • 158