17

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.

Community
  • 1
  • 1
Andrii Yurchuk
  • 3,090
  • 6
  • 29
  • 40

2 Answers2

38

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

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 8
    The problem was that I had Lang3, but the legacy Lang2 was needed. Quite strange that it does not support version 3. – Andrii Yurchuk Oct 04 '11 at 17:58
  • Fair point, I'll edit the answer to include the version numbers as mentioned in the dependencies page. – BalusC Oct 04 '11 at 18:00
  • @Andriy Yurchuk - Apache has a new way to do properties file for Lang3. Please see my 'answer' below. I know its a few years later, but hopefully it may be helpful to someone struggling with this same kind of issue. – joshpt May 23 '17 at 15:07
1

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
joshpt
  • 733
  • 1
  • 7
  • 17