我的代码如下:
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对此代码有两个问题:
package org.minuteware.jgun;
行The type org.apache.commons.lang.exception.NestableException cannot be resolved. It is indirectly referenced from required .class files
} catch (ConfigurationException e) {
行,它显示No exception of type ConfigurationException can be thrown; an exception type must be a subclass of Throwable
我找到ConfigurationException in Java?,但那里提供的解决方案没有帮助。
答案 0 :(得分:38)
Apache Commons Configuration的核心有以下runtime dependencies:
也将它们放入类路径中。您的特定问题是由缺少Lang依赖性引起的。
答案 1 :(得分:0)
这个库问题困扰了我几天,直到我弄清楚为什么Apache希望我使用旧库。
如果要求编译器使用旧的Lang库,请确保以新方式创建Apache属性文件,而不是旧方式(使用较旧的lang库)。 https://commons.apache.org/proper/commons-configuration/userguide/howto_filebased.html 是我从以下代码派生的Apache站点,它对我的Windows机器上的文件执行基本的SET操作。
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