How a JAR file can read an external properties file

后端 未结 7 1112
星月不相逢
星月不相逢 2020-12-24 10:01

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 .proper

7条回答
  •  被撕碎了的回忆
    2020-12-24 10:13

    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
         ...
    }
    

提交回复
热议问题