2

I 'v created a project that reads some of configuration from .properties file

public class PreferenceManager {
    private int refreshTime;
    private String[] filters;
    Properties properties ;
    public PreferenceManager() throws FileNotFoundException, IOException
    {
        properties = new Properties();


        properties.load(ClassLoader.getSystemResourceAsStream ("preferences.properties"));

    }


    public void save() throws IOException, URISyntaxException
    {
        properties.setProperty("REFRESH_TIME", String.valueOf(refreshTime));
        String filtersStr = "";
        if(filters!= null){
            for(String str : filters)
            {
                if((str == null)||(str.isEmpty())) continue;
                filtersStr+= str.toUpperCase()+",";
            }
        }
        properties.setProperty("FILTERS", filtersStr);
        URI uri = ClassLoader.getSystemResource("preferences.properties").toURI();
        File f = new File(uri);
        properties.store(new  FileOutputStream(f), null);
    }
}

and every thing is ok . now I need to create a JAR file. I need to know how to make this JAR file read this properties file from the folder that contains it, because when the properties file is inside the JAR I can read it but I can write on it (Exception : URI is not hirarchal)

so I need ur Help please.

Thanks

1
  • I experienced the same issue. I couldn't figure it out. I played around with a project called one-jar but it doesn't support non-jar resource loading. Eventually, I broke down and now I generate property files on the fly, outside of my .jar.
    – djangofan
    Commented Aug 3, 2011 at 23:17

1 Answer 1

7

Simply store the file in the user's home directory, which is always available, be it a Windows or Linux/Mac machine:

// Initially load properties from jar
Properties props = new Properties();
properties.load(ClassLoader.getSystemResourceAsStream ("preferences.properties"));

// .. work with properties

// Store them in user's home directory
File userHome = new File(System.getProperty("user.home"));
File propertiesFile = new File(userHome, "preferences.properties");

props.store(new FileOutputStream(propertiesFile, "Properties for MyApp");

Next time, when the application starts you want to load them from user's home directory, of course, if the Properties file exists there.
Compare https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/System.html

5
  • Thanks Andreas, It works with me, but I had to use System.getProperty("user.home") not System.getEnv.
    – Jacob
    Commented Aug 4, 2011 at 0:05
  • Thanks, too, for pointing that out. What came to my mind additionally, you can query environment properties defined by you, commpare JAVA_HOME or M2 settings for JDK and Maven. Some application containers like JBoss feature the definition of envirnomental variables within the container. Commented Aug 4, 2011 at 7:31
  • How do I read the file from the working directory where the jar file is , instead from the home directory.
    – Neil
    Commented May 28, 2012 at 10:51
  • the System doc is down. You can find the old 1.4.2 version here (web archive) and the new (or at least newer) version of 1.8.1 here
    – BlueWizard
    Commented Sep 2, 2016 at 11:43
  • btw this answer really helped me. +1
    – BlueWizard
    Commented Sep 4, 2016 at 12:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.