简体   繁体   English

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

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

First of, my sincere apologies for bringing up an oft repeated question in this forum; 首先,我诚挚的歉意在这个论坛上提出一个经常重复的问题。 but I cannot figure out my mistake(s). 但我无法弄清楚自己的错误。

I have two .properties files that I am trying to load unsuccessfully. 我有两个试图加载失败的.properties文件。 Here's the folder structure I have - unless there is a compelling reason otherwise or it is contrary to the best practice, I like to keep this structure: 这是我拥有的文件夹结构-除非有其他令人信服的原因,否则与最佳实践相反,我喜欢保留此结构: 资料夹结构

As you notice my DAO code is under zencs.dbutils package and my .properties files are respectively under zencs.resources.properties.db* packages. 如您所见,我的DAO代码位于zencs.dbutils软件包下,而我的.properties文件分别位于zencs.resources.properties.db *软件包下。

The reason I do it this way because eventually this will connect to and manage multiple data sources - my DAO code will evolve to handle them dynamically (not yet so). 之所以这样做,是因为最终它将连接并管理多个数据源-我的DAO代码将演变为动态处理它们(尚不如此)。 I want to set up all data source properties in one place 我想在一处设置所有数据源属性

My Project properties are set as follows: 我的项目属性设置如下: 项目属性

Now in my DAO class I have a method initProperties(), called by getConnection(), that is trying to reference these properties files through getResourceAsStream(). 现在在我的DAO类中,有一个方法initProperties(),由getConnection()调用,该方法试图通过getResourceAsStream()引用这些属性文件。 Please see below code that I tried: 请参阅以下我尝试过的代码:

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);
    }
}

} }

When I am executing it, the error says "snowflakeConnect.properties file not found! Terminating Application normally..." 当我执行它时,错误提示“找不到snowflakeConnect.properties文件!正在正常终止应用程序...”

My evaluation is that the code in the above form resolving the files to be in zencs/dbutils/ and the ClassLoader cannot find them there obviously. 我的评估是,上述形式的代码将文件解析为zencs / dbutils /,而ClassLoader显然找不到它们。

I tried full absolute path (out of desperation though it expects relative); 我尝试了完整的绝对路径(尽管绝望地期望相对) I tried relative path with "../resources/properties/{dbdrivers | dbutils}/filename.properties" with no success. 我尝试使用“ ../resources/properties/{dbdrivers | dbutils} /filename.properties”创建相对路径,但没有成功。 With the relative path it is resolving to "zencs/dbutils/../resources/properties/dbdrivers/snowflakeConnect.properties" for ClassLoader... 通过相对路径,它可以解析为ClassLoader的“ zencs / dbutils /../ resources / properties / dbdrivers / snowflakeConnect.properties”。

Am I NOT setting the resources folder and everything underneath it correctly? 我没有正确设置资源文件夹及其下面的所有内容吗?

Obviously my comprehension of how it should resolve is flawed. 显然,我对应如何解决的理解是有缺陷的。 Can you please help with what I might have not understood and how should I go about this issue? 您能为我可能不了解的问题提供帮助吗,我应该如何处理该问题?

Thanks a bunch! 谢谢一群!

You could try to use getResourceAsStream() including your package name like this: 您可以尝试使用getResourceAsStream()包括您的软件包名称,如下所示:

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

The leading slash is usually the key part here. 斜线通常是此处的关键部分。 It could help to remove that as well but you said you've tried that already so I guess that's not what you're looking for. 也可以删除它,但是您说您已经尝试过了,所以我想这不是您想要的。

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

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