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:
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
} 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 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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With