Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Eclipse项目中将.properties文件放在何处?_Java_Eclipse_Properties - Fatal编程技术网

Java 在Eclipse项目中将.properties文件放在何处?

Java 在Eclipse项目中将.properties文件放在何处?,java,eclipse,properties,Java,Eclipse,Properties,我有一个非常简单的属性文件测试,我正在尝试工作:(以下是TestProperties.java) 和TestProperties.properties位于同一目录中: something=this is something something.else=this is something else 我还将其保存为TestProperties\u en\u US.properties 当我从Eclipse运行TestProperties.java时,它找不到属性文件: java.util.Mis

我有一个非常简单的属性文件测试,我正在尝试工作:(以下是TestProperties.java)

和TestProperties.properties位于同一目录中:

something=this is something
something.else=this is something else
我还将其保存为
TestProperties\u en\u US.properties

当我从Eclipse运行TestProperties.java时,它找不到属性文件:

java.util.MissingResourceException: 
Can't find bundle for base name TestProperties, locale en_US

我做错了什么吗?

将它放在一个源路径的根级别,或者在调用
getBundle
时完全限定资源名称,例如

ResourceBundle myResources =
          ResourceBundle.getBundle("com.example.test.TestProperties");

有关更多信息,请参阅文档。

啊哈,非常感谢。这也行得通

package com.example.test;

import java.util.ResourceBundle;

public class TestProperties {
    public static void main(String[] args) {
        ResourceBundle myResources =
           ResourceBundle.getBundle(TestProperties.class.getCanonicalName());
        for (String s : myResources.keySet())
        {
            System.out.println(s);
        }
    }
}

我也一直在尝试解决这个问题,我发现在运行项目之前必须刷新Eclipse的文件列表。然后,您可以将文件放在基本目录中并正常使用。

将TestProperties\u en\u US.properties(propery)文件放在src文件夹中,然后运行它将运行的程序。

不要将您的properties文件放在src文件夹中!显然,这是可行的,但基本上这不是你应该如何处理你的问题。在项目中创建一个新文件夹,例如“Resources”文件夹,将其添加到项目属性中的类路径中,并将除.java以外的所有文件放在该文件夹中

package com.example.test;

import java.util.ResourceBundle;

public class TestProperties {
    public static void main(String[] args) {
        ResourceBundle myResources =
           ResourceBundle.getBundle(TestProperties.class.getCanonicalName());
        for (String s : myResources.keySet())
        {
            System.out.println(s);
        }
    }
}