java - java中不读取config.properties文件

标签 java config

我的文件夹结构如下:

>test
    >src
         >org.mypackage
                >myclass
    >resources
         >config.properties

java类如下:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;    

public myclass {

     private String path = "config.properties";

     public myclass() {
         Properties prop = new Properties();
         InputStream input = null;
         try {
             input = getClass().getClassLoader().getResourceAsStream(path);
             prop.load(input)
         }
         catch(Exception ex) {
             ex.printStackTrace();
         }
    }
}

但是在调试期间我得到 null 作为 input 的值。

在 Debug模式下,我检查了 getClass() 的值,得到了类值,该值也可用于 getClass().getClassLoader() 但是对于 getClass().getClassLoader().getResourceAsStream(path) 来说,它是 null

我不确定问题是什么。

感谢您的帮助!

最佳答案

似乎 resources 不是 Eclipse 项目中的源文件夹。尝试将 resources 设置为源文件夹:

Right click > Build Path > Use as Source Folder

关于java - java中不读取config.properties文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40465787/

相关文章:

java - 如果我通过 Spark-Submit 向集群提交非 Spark jar 会发生什么?

java - 如何在 JSOUP 中禁用转义模式?

java - 如何解决 Firebase 聊天应用程序中的错误

java - boolean 测试在应该为真时返回假?

java - 线程局部变量的副本

linux - 即使存在 .config 也使内核提示配置选项

java - Spring mvc 模块使用其他模块的资源文件

java - 我需要如何配置 PersistenceJPAConfig?

linux - 通过文本文件调用脚本(程序名称)

git - .ssh 配置亚马逊 ec2 和 git