首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >正确使用Apache Commons配置

正确使用Apache Commons配置
EN

Stack Overflow用户
提问于 2011-10-05 01:32:12
回答 2查看 21.6K关注 0票数 17

我的代码如下:

代码语言:javascript
复制
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的这段代码有两个问题:

这条线路,它说The type org.apache.commons.lang.exception.NestableException cannot be resolved. It is indirectly referenced from required .class files

  • For,package org.minuteware.jgun;} catch (ConfigurationException e) {,it,,
  1. ,it,package org.minuteware.jgun;} catch (ConfigurationException e) {

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-05 01:40:07

Apache Commons Configuration的核心有以下runtime dependencies

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

票数 38
EN

Stack Overflow用户

发布于 2017-05-23 23:05:28

这个库问题困扰了我几天,直到我弄明白为什么Apache要我使用旧的库。

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

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7651799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档