How a JAR file can read an external properties file

后端 未结 7 1052
星月不相逢
星月不相逢 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: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);
    

提交回复
热议问题