![](/img/trans.png)
[英]Eclipse: How to export a .jar file that has a .jar file in the build path
[英]How to build jar using Eclipse “Export as jar option” with a properties file
public xFbConfigReader()
{
//props = new Properties();
propsdatabase = new Properties();
try
{
// load a properties file
InputStream dbin = getClass().getResourceAsStream("/properties/database.properties");
propsdatabase.load(dbin);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
我将名为“ Database.properties”的属性文件保存在项目名为“属性”的文件夹中。
当我在Eclipse中执行Export as jar时。 “属性”文件夹可见。
但是,当我运行程序时,它表明dbin中存在NUll点异常。
所以这意味着我需要在Eclipse中形成jar的正确方法。
处理属性文件时更好的解决方案是读取
static Properties databaseproperties= new Properties();
static {
try {
connectionProps.load(YourClassName.class.getClassLoader()
.getResourceAsStream("databaseproperties.properties"));
} catch (Exception e) {
System.out.println("Exception is " + e.getMessage());
}
}
这是更好的方法,因为
(OR)只需进行更改即可,无需考虑目录结构
步骤2:如下更改此行
InputStream dbin = getClass()。getResourceAsStream(“ / database.properties”);
这与以前的代码没有太大区别,因为它始终保留在JAR文件中。
您将收到空指针异常,因为未加载属性文件,请尝试使用FileInputStream加载属性,如下所示:FileInputStream dbin = new FileInputStream(“ / properties / database.properties”); properties.load(dbin);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.