【问题标题】:Proper usage of Apache Commons Configuration正确使用 Apache Commons 配置
【发布时间】:2017-05-23 23:05:28
【问题描述】:

我的代码如下:

package org.minuteware.jgun;

import org.apache.commons.configuration.*;

class ConfigReader {
    public void getconfig() {
        Configuration config;
        try {
            config = new PropertiesConfiguration("gun.conf");
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
        String day = config.getString("sync_overlays");
        System.out.println(day);
    }
}

Eclipse 对这段代码有两个问题:

  1. 对于package org.minuteware.jgun; 行,它显示The type org.apache.commons.lang.exception.NestableException cannot be resolved. It is indirectly referenced from required .class files
  2. 对于} catch (ConfigurationException e) { 行,它显示No exception of type ConfigurationException can be thrown; an exception type must be a subclass of Throwable

我找到了ConfigurationException in Java?,但那里提供的解决方案没有帮助。

【问题讨论】:

    标签: java configuration


    【解决方案1】:

    Apache Commons Configuration的核心有以下runtime dependencies

    也将它们放入您的类路径中。您的特定问题是由缺少 Lang 依赖项引起的。

    【讨论】:

    • 问题是我有 Lang3,但需要旧版 Lang2。很奇怪它不支持版本 3。
    • 公平点,我将编辑答案以包含依赖项页面中提到的版本号。
    • @Andriy Yurchuk - Apache 有一种新的方式来为 Lang3 做属性文件。请在下面查看我的“答案”。几年后我才知道,但希望它对那些在同样问题上苦苦挣扎的人有所帮助。
    【解决方案2】:

    这个库问题困扰了我几天,直到我弄清楚 Apache 为何要我使用旧库。

    如果编译器要求您使用较旧的 Lang 库,请确保您使用新方式而不是旧方式(使用较旧的 lang 库)来制作 Apache 属性文件。 https://commons.apache.org/proper/commons-configuration/userguide/howto_filebased.html 是我从中派生以下代码的 Apache 站点,它对我的​​ Windows 机器上的文件执行基本的 SET 操作。

    import org.apache.commons.configuration2.Configuration;
    import org.apache.commons.configuration2.FileBasedConfiguration;
    import org.apache.commons.configuration2.PropertiesConfiguration;
    import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
    import org.apache.commons.configuration2.builder.fluent.Parameters;
    
    public final class Settings implements Serializable {
    
    private Configuration config;
    private String propertiesFilePath;
    private FileBasedConfigurationBuilder<FileBasedConfiguration> builder;
    
    public Settings(String propertiesFilePath) {
        Parameters params = new Parameters();
        File propFile = new File(propertiesFilePath);
        builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
                .configure(params.fileBased()
                        .setFile(propFile));
        try {
            config = builder.getConfiguration();
        } catch (Exception e) {
            System.out.println("Exception - Settings constructor: " + e.toString());
        }
    }//end constructor
    
    public void setValue(String key, String value) throws Exception {
            config.setProperty(key, value);
            builder.save();
     }// end setter method
    }//end class
    

    【讨论】:

      猜你喜欢
      • 2012-01-28
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 2013-02-26
      • 2012-12-13
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      相关资源
      最近更新 更多