How a JAR file can read an external properties file

后端 未结 7 1053
星月不相逢
星月不相逢 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:09

    Simplest way is below. It will load application.properties from cfg folder outside of the jar file.

    Directory Structure

      |-cfg<Folder>-->application.properties
      |-somerunnable.jar
    

    Code:

        Properties mainProperties = new Properties();
        mainProperties.load(new FileInputStream("./cfg/application.properties"));
        System.out.println(mainProperties.getProperty("error.message"));
    
    0 讨论(0)
  • 2020-12-24 10:10

    In netbeans I needed to load application.properties from conf/ folder outside of the jar file.

    Therefore I wrote :

    public static String getProperty(String FileName, String Prop)
    {   
    
        try {  
            FIS = new FileInputStream( "./../conf/"+FileName);
            Properties properties;
            (properties = new Properties()).load(FIS);          
    
            for(Enumeration propKeys = properties.propertyNames();
                    propKeys.hasMoreElements();){
                String tmpKey = (String) propKeys.nextElement();
                String tmpValue = properties.getProperty(tmpKey);                   
    
                tmpValue = tmpValue.trim();
                if (tmpKey.equals(Prop)){
                    //System.out.println(tmpKey +"="+tmpValue);
                    properties.put(tmpKey, tmpValue);
                    Value = properties.getProperty(Prop);
                    break;
                }
    
            }
    
            if (Value==null){
                throw new Exception("La Propiedad : "+Prop+" no Se encuentra en el Archivo de Configuracion");
            } 
    
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Value;
    
    }
    

    For Eclipse apply the following:

    FIS = new FileInputStream( "../conf/"+FileName);
    
    0 讨论(0)
  • 2020-12-24 10:12

    http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html

    multiple approaches are available, the article above provides more details

     ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
     Class.getResourceAsStream ("/some/pkg/resource.properties");
     ResourceBundle.getBundle ("some.pkg.resource");
    
    0 讨论(0)
  • 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
         ...
    }
    
    0 讨论(0)
  • 2020-12-24 10:17
    public static String getPropertiesValue(String propValue) {
            Properties props = new Properties();
            fileType = PCLLoaderLQIOrder.class.getClassLoader().getResourceAsStream(propFileName);
            if (fileType != null) {
                try {
                    props.load(fileType);
                } catch (IOException e) {
                    logger.error(e);
                }
            } else {
                try {
                    throw new FileNotFoundException("Property file" + propFileName + " not found in the class path");
                } catch (FileNotFoundException e) {
                    logger.error(e);
                }
            }
            String propertiesValue = props.getProperty(propValue);
            return propertiesValue;
        }
    

    above methods works for me, just store your property file into directory from where to run your jar and provide that name in place of propFileName, when you want any value from property just call getPropertyValue("name").

    0 讨论(0)
  • 2020-12-24 10:21

    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"));
    
    0 讨论(0)
提交回复
热议问题