java - Include A Properties File With A Jar File -


i've written small application. i've put database specific information in properties file.

db.url=jdbc:mysql://localhost:3306/librarydb db.user=root db.passwd=pas$w0rd 

when build application executable jar file, properties file gets included inside. whole purpose of putting information in properties file allow user change making impossible.

how can include properties file jar file not in it? kinda appsettings file in .net applications.

i'm new java i'd appreciate if break down set of steps.

thank you.

edit :

this how i'm getting values properties file in program.

public class databasehandler {      public static connection connecttodb() {         connection con = null;         properties props = new properties();         inputstream in = null;          try {             in = databasehandler.class.getresourceasstream("database.properties");             props.load(in);             in.close();              string url = props.getproperty("db.url");             string user = props.getproperty("db.user");             string password = props.getproperty("db.passwd");              con = drivermanager.getconnection(url, user, password);         } catch (exception ex) {                     logger.getlogger(databasehandler.class.getname()).log(level.severe, null, ex);          }             return con;     }  } 

you right. if properties intended modifiable user, putting them in properties file in jar not going work

there couple of approaches:

  • you put properties in property file store in file system. problem figuring out where in filesystem store them:

    • on unix / linux there conventions store system-wide , per-user configuration properties, these bit loose, , in bit of state of flux (e.g. "desktop" frameworks have own preference mechanisms.

    • there similar conventions windows believe.

    • the problem approach can permissions issues (you don't want 1 user changing another's preferences), , possibly issues read-only file systems , like.

    • if going use property file in filesystem, bad idea hardwire location code. make configurable via command line argument and/or "system property" can set using -d option.

  • on windows, put properties windows registry. need use 3rd party libraries this. , of course, this not portable - won't work on non-windows platform.

  • you use java preferences api. portable across multiple platforms java-centric solution; i.e. doesn't fit platform's "normal way" of doing things.

in short, there no solution ideal perspectives.


but once you've decided how store preferences, could write application uses properties file in jar file provide default or initial settings.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -