Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper usage of Apache Commons Configuration

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

like image 963
Andrii Yurchuk Avatar asked Oct 04 '11 17:10

Andrii Yurchuk


People also ask

What is the use of Apache Commons?

The Apache Commons is a project of the Apache Software Foundation, formerly under the Jakarta Project. The purpose of the Commons is to provide reusable, open source Java software. The Commons is composed of three parts: proper, sandbox, and dormant.

What is Apache Commons configuration?

Apache Commons Configuration is a java library that simplifies managing application configuration properties. It allows you to collect properties from different configuration sources like properties files, XML files, Java System properties, Environemnt variables etc.


2 Answers

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.

like image 57
BalusC Avatar answered Oct 14 '22 11:10

BalusC


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
like image 44
joshpt Avatar answered Oct 14 '22 10:10

joshpt