【问题标题】:How a JAR file can read an external properties fileJAR 文件如何读取外部属性文件
【发布时间】:2010-08-11 11:51:39
【问题描述】:

我们的一个应用程序有一个连接池组件(JAR 文件)。截至目前,应用程序连接详细信息已捆绑在 JAR 文件中(在 .properties 文件中)。

我们可以让它更通用吗?我们可以让客户端告诉属性文件详细信息(路径和文件名)并使用 JAR 获取连接吗?

在客户端代码中有这样的东西有意义吗:

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

除此之外,客户端将指定(不知何故???)具有要连接的 URL、超时等的属性文件详细信息。

【问题讨论】:

    标签: java properties jar


    【解决方案1】:

    只需从文件中加载属性,例如

    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) {}
         }
    }
    

    请注意上面的编码是如何明确指定为 UTF-8 的。如果您接受默认的 ISO8859-1 编码,也可以省略它,但请注意任何特殊字符。

    【讨论】:

      【解决方案2】:

      这是我的解决方案。首先在启动文件夹中查找app.properties,如果不存在尝试从您的JAR 包中加载:

      File external = new File("app.properties");
      if (external.exists())
          properties.load(new FileInputStream(external));
      else 
          properties.load(Main.class.getClassLoader().getResourceAsStream("app.properties"));
      

      【讨论】:

      • 这很简单,很好,对我的宠物项目很有帮助。
      【解决方案3】:

      最简单的方法,使用 -D 开关在 java 命令行上定义系统属性。 该系统属性可能包含您的属性文件的路径。

      例如

      java -cp ... -Dmy.app.properties=/path/to/my.app.properties my.package.App
      

      然后,您可以在您的代码中执行(为简洁起见,未显示异常处理):

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

      【讨论】:

        【解决方案4】:

        最简单的方法如下。它将从 jar 文件之外的 cfg 文件夹中加载 application.properties。

        目录结构

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

        代码:

            Properties mainProperties = new Properties();
            mainProperties.load(new FileInputStream("./cfg/application.properties"));
            System.out.println(mainProperties.getProperty("error.message"));
        

        【讨论】:

          【解决方案5】:

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

          有多种方法可供选择,上面的文章提供了更多详细信息

           ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
           Class.getResourceAsStream ("/some/pkg/resource.properties");
           ResourceBundle.getBundle ("some.pkg.resource");
          

          【讨论】:

            【解决方案6】:
            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;
                }
            

            以上方法对我有用,只需将您的属性文件存储到运行 jar 的目录中并提供该名称来代替 propFileName,当您想从属性中获取任何值时,只需调用 getPropertyValue("name")

            【讨论】:

            • 难以辨认。
            【解决方案7】:

            在 netbeans 中,我需要从 jar 文件外的 conf/ 文件夹加载 application.properties。

            所以我写了:

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

            对于 Eclipse,应用以下内容:

            FIS = new FileInputStream( "../conf/"+FileName);
            

            【讨论】:

              猜你喜欢
              • 2016-11-29
              • 2017-11-14
              • 2012-02-05
              • 1970-01-01
              • 2019-04-23
              • 2017-06-04
              • 2012-10-28
              • 2015-12-23
              • 2012-12-25
              相关资源
              最近更新 更多