简体   繁体   English

无法在Java路径中找到属性文件

[英]cannot locate properties file in java path

My code cannot locate the .properties file where i have stored login information. 我的代码无法找到我存储登录信息的.properties文件。

I have put the file in the src folder to make sure it compiles, and it does correctly. 我将文件放在src文件夹中,以确保它可以编译并正确执行。

below is the current location of the file and how i am trying to access it. 以下是文件的当前位置以及我如何尝试访问它。

I have tried various different paths but no luck. 我尝试了各种不同的方法,但是没有运气。

在此处输入图片说明

Change your code; 更改您的代码;

 ResourceBundle bundle = ResourceBundle.getBundle("Selenium/readme");

to

 ResourceBundle bundle = ResourceBundle.getBundle("readme");

You don't compile a .properties file, you use it as is. 您不编译.properties文件,而是按原样使用它。

If you use a FileInputStream it will use the working directory which is set in your Run configuration (most likely the top directory) 如果使用FileInputStream,它将使用在运行配置中设置的工作目录(最有可能是顶层目录)

But you are loading it as a resource which means it must be in your class path. 但是您将其作为资源加载,这意味着它必须在您的类路径中。 The simplest thing to do is to create a sub-directory for your configuration and add this to your programs class path. 最简单的操作是为您的配置创建一个子目录,并将其添加到程序的类路径中。

Read properties file like: 读取属性文件,例如:

ResourceBundle.getBundle("src/properties/readme.properties"); //Or simply "properties/readme.properties"

Put readme.properties under src/properties directory. 将readme.properties放在src/properties目录下。

you can use this.getClass().getResourceAsStream("readme.properties"); 您可以使用this.getClass().getResourceAsStream("readme.properties");

for more information read: 有关更多信息,请阅读:

I see that the .properties file is not inside the src folder. 我看到.properties文件不在src文件夹内。 Also check the build path of your project.It will show you the src folders and the output folders location. 还要检查项目的构建路径,它将显示src文件夹和输出文件夹的位置。 Once you build the project using eclipse build project option, make sure your properties file is now available in the output folder. 使用eclipse build project选项构建项目后,请确保您的属性文件现在在输出文件夹中可用。

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

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