我有一个具有以下结构的项目:
myPoject
src
com.config
PropertiesFile.java
com.main
Main.java
resources
data.png
config.properties
public class PropertiesFile {
private static Logger logger = LogManager.getLogger(PropertiesFile.class);
private static final char CONFIG_DELIMITER = ',';
private static final String CONFIG = "config.properties";
private static org.apache.commons.configuration2.Configuration config;
static {
try {
Parameters params = new Parameters();
FileBasedConfigurationBuilder<FileBasedConfiguration> builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
.configure(params.properties()
.setFileName(CONFIG)
.setListDelimiterHandler(new DefaultListDelimiterHandler(CONFIG_DELIMITER)));
config = builder.getConfiguration(); }
catch (ConfigurationException cE) {
logger.fatal("Error", cE);
}
}
public static final String ID = config.getString("id");
public static final String DEST_HOST = config.getString("destination.host");
public static final int DEST_PORT = config.getInt("destination.port");
}
config.properties
id = myApp
destination.host = localhost
destination.port = 9898
Main.java
public class Main{
public static void main(String[] args) throws InterruptedException {
Thread tc = new Thread(new Runnable() {
@Override
public void run() {
connect();
}
});
tc.start();
tc.join();
}
private static void connect() {
try (Socket socket = new Socket(PropertiesFile.DEST_HOST, PropertiesFile.DEST_PORT)) {
//...code
}
}
当我在Eclipse中运行时,它工作正常,但是当我制作jar并尝试执行时,出现此错误
2018年2月28日上午10:23:28 org.apache.commons.beanutils.FluentPropertyBeanIntrospector内省
信息:为公共最终无效org.apache.commons.config创建PropertyDescriptor时出错
uration2.AbstractConfiguration.setProperty(java.lang.String,java.lang.Object)!忽略此属性。
线程“ Thread-1”中的异常java.lang.ExceptionInInitializerError
在com.main.Main.connect(Main.java:47)
在com.main.Main.access $ 0(Main.java:46)
在com.main.Main $ 1.run(Main.java:37)
在java.lang.Thread.run(未知来源)
原因:java.util.NoSuchElementException:键'destination.port'不映射到现有对象!
在org.apache.commons.configuration2.AbstractConfiguration.throwMissingPropertyException(AbstractConfiguration.java:1911)
在org.apache.commons.configuration2.AbstractConfiguration.checkNonNullValue(AbstractConfiguration.java:1898)
t org.apache.commons.configuration2.AbstractConfiguration.getInt(AbstractConfiguration.java:1262)
位于com.config.PropertiesFile。(PropertiesFile.java:43)
...还有5个
jar是否看不到config.properties文件?
答案 0 :(得分:0)
文件config.properties
应该位于资源文件夹下。