1

I am trying to set up a java .properties file outside of the packaged jar. This is my code to load it:

public static final String FILENAME = "test.properties";

public static void load() throws IOException {
    FileInputStream fis = null;
    try {
        props = new Properties();
        fis = new FileInputStream(FILENAME);
        props.load(fis);
        System.out.println("Properties successfully loaded: "+props);
        validateProperties();
    } catch (FileNotFoundException e) {
        System.err.println("Properties file not found.  Creating...");
        new File(FILENAME).createNewFile();
                    //fill with default properties
        System.out.println("Properties file successfully created");
    } finally {
        if (fis != null) try {fis.close();} catch(Exception e) {}
    }
}

Unfortunately, when I run this, I get the following output:

Properties successfully loaded: {}

Here is test.properties:

#no comment
#Sun Jun 23 19:21:45 CDT 2013
port=55142
handSize=10
maxPlayers=8
timeout=1500

I have confirmed, by manually reading and printing, that the FileInputStream is reading from the correct file. So why aren't my properties loading?

EDIT: Here is some code which loads the contents of the properties file directly:

public static void test() throws IOException {
    FileInputStream fis = new FileInputStream(FILENAME);
    byte[] b = new byte[fis.available()];
    fis.read(b);
    String text = new String(b);
    System.out.println(text);
}

and it outputs:

#no comment
#Sun Jun 23 19:21:45 CDT 2013
port=55142
handSize=10
maxPlayers=8
timeout=500

so the FIS must be reading from the correct file.

EDIT 2: Ok, so I don't know what the problem was, but I restarted eclipse and now it's working. Very sorry to have wasted your time.

2
  • It sounds like the properties file is not in the same location that the program is running from. Make sure that the program is executed in the same directory as the properties file is stored Commented Jun 24, 2013 at 0:48
  • The file was created by building a default property set and saying props.store(FILENAME). Also, I've confirmed that the FileInputStream is reading from the correct file. Commented Jun 24, 2013 at 0:57

4 Answers 4

1

Check what line separator your java system uses. Eg:

System.out.println((int)System.getProperty("line.separator").charAt(0));

On UNIX that will give 10, which is newline \n, on Windows that will be 13 (eg: the first char of \r\n).

I think your java code is reading the file using Windows encoding, yet the property file is edited in UNIX, hence everyting appears to be in "one single line" -- which will result in empty properties because your first line is commented

Sign up to request clarification or add additional context in comments.

Comments

0

As your properties file is not present in the classpath so you cannot read it without giving the proper path. There are multiple approaches to read an external properties file from a jar. One of the simplest way is to use the -D switch to define a system property on a java command line. That system property may contain a path to your properties file.

E.g

java -cp ... -Dmy.app.properties=/path/to/test.properties my.package.App

Then, in your code you can do :

public static final String FILENAME = "test.properties";

public static void load() throws IOException {
    FileInputStream fis = null;
    String propPath = System.getProperty(FILENAME);

    try {
        props = new Properties();
        fis = new FileInputStream(propPath);
        props.load(fis);
        System.out.println("Properties successfully loaded: "+props);
        validateProperties();
    } catch (FileNotFoundException e) {
        System.err.println("Properties file not found.  Creating...");
        new File(propPath ).createNewFile();
                    //fill with default properties
        System.out.println("Properties file successfully created");
    } finally {
        if (fis != null) try {fis.close();} catch(Exception e) {}
    }
}

4 Comments

I want the .properties file to be in the same folder as the jar, so that the path doesn't need to be specified. Also, it is confirmed that the FileInputStream is reading from the correct file.
@MartinWickham But your problem description is confusion "I am trying to set up a java .properties file outside of the packaged jar"
Outside of, but in the same folder as the jar file. Finding the file is not the issue. The problem is that the properties are not loaded from it.
@JunedAhsan, you are trying to set up system property file. This is not what he wants.
0

When there is no absolute path mentioned, JVM tries to load resources from the JVM & project classpath. In your case, empty output signifies that JVM is trying to load property file from classpath but the file is not there.

Solutions:

1) Either place your property file in your classpath

2) Or mention absolute path to property file.

6 Comments

Are you sure that wouldn't throw a FileNotFoundException? because that's what happens when I delete test.properties.
Yes, it will throw filenotfoundexception.
yeah, but that's not what's happening. It finds the file, it just doesn't find any properties within it.
Hmm, can you check what's inside fis object. whether its empty or something in there ?
@RaviTrivedi I don't think your answer is correct. OP's code snippet shows FileInputStream is used, which if no absolute path is specified by default current directory (eg: java user.dir) is used -- not classpath
|
0

A ResourceBundle offers a very easy way to access key/value pairs in a properties file in a Java... You can refer following.

http://www.avajava.com/tutorials/lessons/how-do-i-read-a-properties-file-with-a-resource-bundle.html

You can directly specify your properties file name while loading the bundle when it is present in the same folder as your jar/classes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.