繁体   English   中英

无法使getResourceAsStream()加载.properties资源-使用Netbeans IDE

[英]Unable to make getResourceAsStream() to load the .properties resource - using Netbeans IDE

首先,我诚挚的歉意在这个论坛上提出一个经常重复的问题。 但我无法弄清楚自己的错误。

我有两个试图加载失败的.properties文件。 这是我拥有的文件夹结构-除非有其他令人信服的原因,否则与最佳实践相反,我喜欢保留此结构: 资料夹结构

如您所见,我的DAO代码位于zencs.dbutils软件包下,而我的.properties文件分别位于zencs.resources.properties.db *软件包下。

之所以这样做,是因为最终它将连接并管理多个数据源-我的DAO代码将演变为动态处理它们(尚不如此)。 我想在一处设置所有数据源属性

我的项目属性设置如下: 项目属性

现在在我的DAO类中,有一个方法initProperties(),由getConnection()调用,该方法试图通过getResourceAsStream()引用这些属性文件。 请参阅以下我尝试过的代码:

public class DAO {
Connection conn = null;

public Properties properties = new Properties();
public Properties dbConnect = new Properties();

private void initProperties()  {
    InputStream inputDBdrivers = getClass().getResourceAsStream("snowflakeConnect.properties");
    if (inputDBdrivers != null) {
        try{
            dbConnect.load(inputDBdrivers);
            inputDBdrivers.close();
        } catch(IOException ioex) {
            System.err.println(ioex.getStackTrace().toString());
        }
    } else {
        System.out.println("snowflakeConnect.properties file not found! Terminating Application normally...");
        System.exit(0);
    }
    InputStream inputDBprops = getClass().getResourceAsStream("snowflake.properties");
    if (inputDBprops != null) {
        try{
            properties.load(inputDBprops);
            inputDBprops.close();
        } catch(IOException ioex) {
            System.err.println(ioex.getStackTrace().toString());
        }
    } else {
        System.out.println("snowflake.properties file not found! Terminating Application normally...");
        System.exit(0);
    }
}

Connection getConnection() throws SQLException {
    // build connection properties
    initProperties();
    try {
        Class.forName(dbConnect.getProperty("driver"));
    } catch (ClassNotFoundException cnfex) {
        System.err.println("ERROR: getConnection() :: Snowflake Class not found: " + cnfex.getMessage());
    }

    return DriverManager.getConnection(dbConnect.getProperty("connectStr"), properties);
}

public DAO() {
    try {
      this.conn = getConnection();
    } catch (SQLException sqlex) {
      Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, sqlex);
    }
}

}

当我执行它时,错误提示“找不到snowflakeConnect.properties文件!正在正常终止应用程序...”

我的评估是,上述形式的代码将文件解析为zencs / dbutils /,而ClassLoader显然找不到它们。

我尝试了完整的绝对路径(尽管绝望地期望相对) 我尝试使用“ ../resources/properties/{dbdrivers | dbutils} /filename.properties”创建相对路径,但没有成功。 通过相对路径,它可以解析为ClassLoader的“ zencs / dbutils /../ resources / properties / dbdrivers / snowflakeConnect.properties”。

我没有正确设置资源文件夹及其下面的所有内容吗?

显然,我对应如何解决的理解是有缺陷的。 您能为我可能不了解的问题提供帮助吗,我应该如何处理该问题?

谢谢一群!

您可以尝试使用getResourceAsStream()包括您的软件包名称,如下所示:

InputStream inputDBdrivers = getClass().getResourceAsStream("/zencs/resources/properties/dbdrivers/snowflakeConnect.properties");
InputStream inputDBprops = getClass().getResourceAsStream("/zencs/resources/properties/dbutils/snowflake.properties");

斜线通常是此处的关键部分。 也可以删除它,但是您说您已经尝试过了,所以我想这不是您想要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM