Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How a JAR file can read an external properties file

We have a connection pooling component (JAR file) for one of our application. As of now the application connection details are bundled with-in the JAR file (in .properties file).

Can we make it more generic? Can we have the client tell the properties file details (both the path and the file name) and use the JAR to get the connection?

Does it make sense to have something like this in the client code:

XyzConnection con = connectionIF.getConnection(uname, pwd);

Along with this, the client will specify (somehow???) the properties file details that has the URLs to connect, timeout etc.

like image 905
jai Avatar asked Aug 11 '10 11:08

jai


People also ask

How do you make a jar use an external properties file?

Simplest way, 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. Then, in your code you can do ( exception handling is not shown for brevity ): String propPath = System.

How read external properties file in java with example?

Steps for reading a properties file in JavaCreate an instance of Properties class. Create a FileInputStream by opening a connection to the properties file. Read property list (key and element pairs) from the input stream using load() method of the Properties class.


3 Answers

Just load the properties from file, something like

Properties properties = new Properties();
InputStreamReader in = null;
try {
     in = new InputStreamReader(new FileInputStream("propertiesfilepathandname"), "UTF-8");
     properties.load(in);
} finally {
     if (null != in) {
         try {
             in.close();
         } catch (IOException ex) {}
     }
}

Note how the encoding is explicitly specified as UTF-8 above. It could also be left out if you accept the default ISO8859-1 encoding, but beware with any special characters then.

like image 130
Joonas Pulakka Avatar answered Sep 22 '22 22:09

Joonas Pulakka


This is my solution. First looking for app.properties in startup folder, if does not exists try to load from your JAR package:

File external = new File("app.properties");
if (external.exists())
    properties.load(new FileInputStream(external));
else 
    properties.load(Main.class.getClassLoader().getResourceAsStream("app.properties"));
like image 36
tarn Avatar answered Sep 24 '22 22:09

tarn


Simplest way, 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/my.app.properties my.package.App

Then, in your code you can do ( exception handling is not shown for brevity ):

String propPath = System.getProperty( "my.app.properties" );

final Properties myProps;

if ( propPath != null )
{
     final FileInputStream in = new FileInputStream( propPath );

     try
     {
         myProps = Properties.load( in );
     }
     finally
     {
         in.close( );
     }
}
else
{
     // Do defaults initialization here or throw an exception telling
     // that environment is not set
     ...
}
like image 34
Alexander Pogrebnyak Avatar answered Sep 24 '22 22:09

Alexander Pogrebnyak