开发者

Proper usage of Apache Commons Configuration

开发者 https://www.devze.com 2023-04-10 09:10 出处:网络
My code is the following: package org.minuteware.jgun; import org.apache.commons.configuration.*; class ConfigReader {

My code is the following:

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.开发者_StackOverflow中文版printStackTrace();
        }
        String day = config.getString("sync_overlays");
        System.out.println(day);
    }
}

Eclipse has two problems with this code:

  1. For the package org.minuteware.jgun; line it says The type org.apache.commons.lang.exception.NestableException cannot be resolved. It is indirectly referenced from required .class files
  2. For the line } catch (ConfigurationException e) { it says No exception of type ConfigurationException can be thrown; an exception type must be a subclass of Throwable

I've found ConfigurationException in Java?, but the solution provided there does not help.


The core of Apache Commons Configuration has the following runtime dependencies:

  • Apache Commons Lang (version 2.2, 2.3, 2.4, 2.5 or 2.6)
  • Apache Commons Collections (version 3.1, 3.2 or 3.2.1)
  • Apache Commons Logging (version 1.0.4, 1.1 or 1.1.1)

Put them in your classpath as well. Your particular problem is caused by a missing Lang dependency.


This library issue plagued me for a few days until I figure out why Apache was wanting me to use old libraries.

If you are being requested to use older Lang libraries by the compiler, ensure you are making your Apache properties file the NEW way, not the old way (which utilizes the older lang libraries). https://commons.apache.org/proper/commons-configuration/userguide/howto_filebased.html is the Apache site I derived my following code from, which does a basic SET operation against a file on my Windows machine.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号