简体   繁体   English

java.util.MissingResourceException:找不到基本名称'property_file name'的捆绑包,语言环境en_US

[英]java.util.MissingResourceException: Can't find bundle for base name 'property_file name', locale en_US

I am trying to create a utility class ReadPropertyUtil.java for reading data from property file.我正在尝试创建一个实用程序 class ReadPropertyUtil.java用于从属性文件中读取数据。 While my class is located under a util directory, my skyscrapper.properties file is placed in some other directory.虽然我的 class 位于 util 目录下,但我的skyscrapper.properties文件位于其他目录中。

But, when i try to access the properties using [ResourceBundle][1] , i get exceptions, that bundle can't be loaded.但是,当我尝试使用[ResourceBundle][1]访问属性时,出现异常,无法加载该包。

Below is the code on how I am reading the properties and also an image which shows my directory structure.下面是关于我如何读取属性的代码以及显示我的目录结构的图像。

ReadPropertiesUtil.java读取属性Util.java

/**
 * Properties file name.
 */
private static final String FILENAME = "skyscrapper";

/**
 * Resource bundle.
 */
private static ResourceBundle resourceBundle = ResourceBundle.getBundle(FILENAME);

/**
 * Method to read the property value.
 * 
 * @param key
 * @return
 */
public static String getProperty(final String key) {
    String str = null;
    if (resourceBundle != null) {
        str = resourceBundle.getString(key);
            LOGGER.debug("Value found: " + str + " for key: " + key);
    } else {
            LOGGER.debug("Properties file was not loaded correctly!!");
    }
    return str;
}

Directory Structure目录结构

在此处输入图像描述

This line is giving the error private static ResourceBundle resourceBundle = ResourceBundle.getBundle(FILENAME);此行给出错误private static ResourceBundle resourceBundle = ResourceBundle.getBundle(FILENAME);

I am unable to understand why isn't this working and what is the solution.我无法理解为什么这不起作用以及解决方案是什么。 The src folder is already added in build path completely. src文件夹已经完全添加到构建路径中。

尝试使用资源的完全限定名称

private static final String FILENAME = "resources/skyscrapper";

ResourceBundle doesn't load files? ResourceBundle不加载文件? You need to get the files into a resource first.您需要先将文件放入资源中。 How about just loading into a FileInputStream then a PropertyResourceBundle如何只加载到FileInputStream然后是PropertyResourceBundle

   FileInputStream fis = new FileInputStream("skyscrapper.properties");
   resourceBundle = new PropertyResourceBundle(fis);

Or if you need the locale specific code, something like this should work或者,如果您需要特定于语言环境的代码,这样的东西应该可以工作

File file = new File("skyscrapper.properties");
URL[] urls = {file.toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
ResourceBundle rb = ResourceBundle.getBundle("skyscrapper", Locale.getDefault(), loader);

Use the Resource like像这样使用资源

ResourceBundle rb = ResourceBundle.getBundle("com//sudeep//internationalization//MyApp",locale);
or
ResourceBundle rb = ResourceBundle.getBundle("com.sudeep.internationalization.MyApp",locale);

Just give the qualified path .. Its working for me!!!只需给出合格的路径..它对我有用!!!

您应该设置不带.properties扩展名的属性文件名,它对我来说可以正常工作:)

I'd like to share my experience of using Ant in building projects, *.properties files should be copied explicitly .我想分享我在构建项目中使用 Ant 的经验, *.properties 文件应该被显式复制 This is because Ant will not compile *.properties files into the build working directory by default (javac just ignore *.properties).这是因为 Ant 默认不会将 *.properties 文件编译到构建工作目录中(javac 只是忽略 *.properties)。 For example:例如:

<target name="compile" depends="init">
    <javac destdir="${dst}" srcdir="${src}" debug="on" encoding="utf-8" includeantruntime="false">
        <include name="com/example/**" />
        <classpath refid="libs" />
    </javac>
    <copy todir="${dst}">
        <fileset dir="${src}" includes="**/*.properties" />
    </copy>
</target>

<target name="jars" depends="compile">
    <jar jarfile="${app_jar}" basedir="${dst}" includes="com/example/**/*.*" />
</target>

Please notice that 'copy' section under the 'compile' target, it will replicate *.properties files into the build working directory.请注意“编译”目标下的“复制”部分,它会将 *.properties 文件复制到构建工作目录中。 Without the 'copy' section the jar file will not contain the properties files, then you may encounter the java.util.MissingResourceException.如果没有“复制”部分,jar 文件将不包含属性文件,那么您可能会遇到 java.util.MissingResourceException。

The simplest code would be like, keep your properties files into resources folder, either in src/main/resource or in src/test/resource.最简单的代码是,将属性文件保存到资源文件夹中,或者在 src/main/resource 或 src/test/resource 中。 Then use below code to read properties files:然后使用以下代码读取属性文件:

public class Utilities {
    static {
        rb1 = ResourceBundle.getBundle("fileNameWithoutExtension"); 
              // do not use .properties extension
    }
    public static String getConfigProperties(String keyString) {
        return rb1.getString(keyString);
    }
}

With Eclipse and Windows:使用 Eclipse 和 Windows:

you have to copy 2 files - xxxPROJECTxxx.properties - log4j.properties here : C:\Eclipse\CONTENER\TOMCAT\apache-tomcat-7\lib您必须在此处复制 2 个文件 - xxxPROJECTxxx.properties - log4j.properties:C:\Eclipse\CONTENER\TOMCAT\apache-tomcat-7\lib

I have just realized that my error was caused in the naming convention of my property file.我刚刚意识到我的错误是在我的属性文件的命名约定中引起的。 When i used xxxx.xxxx.properties i got the error:当我使用 xxxx.xxxx.properties 时出现错误:

java.util.MissingResourceException: Can't find bundle for base name 'property_file name', locale en_US java.util.MissingResourceException:找不到基本名称'property_file name'的包,语言环境en_US

Changing it to something like xxx-xxxx.properties works like a charm.将其更改为 xxx-xxxx.properties 之类的东西就像一个魅力。 Hope i help someone!希望我能帮助别人!

只需右键单击 Eclipse 中的项目文件并在构建路径中选择“用作源文件夹”...它对我有用

You can try anyone with resources-您可以尝试任何有资源的人-

private static final String FILENAME = "resources.skyscrapper";
private static final String FILENAME = "resources/skyscrapper";
private static final String FILENAME = "resources\\skyscrapper";
private static final String FILENAME = "resources//skyscrapper";

By default it tries to find 'skyscrapper.properties' file inside 'src' but you have placed your file inside a sub-directory 'resources' which is unreachable.默认情况下,它会尝试在“src”中查找“skyscrapper.properties”文件,但您已将文件放在无法访问的子目录“resources”中。

In a maven project-在一个 Maven 项目中-

It looks like- 'src/main/resources/skyscrapper.properties' then use-它看起来像 - 'src/main/resources/skyscrapper.properties' 然后使用 -

private static final String FILENAME = "skyscrapper";

And if it looks like- 'src/main/resources/prop/xyz/skyscrapper.properties' then use-如果它看起来像 - 'src/main/resources/prop/xyz/skyscrapper.properties' 然后使用 -

private static final String FILENAME = "prop/xyz/skyscrapper";
private static final String FILENAME = "prop.xyz.skyscrapper";

In this case by default it tries to find 'skyscrapper.properties' file inside 'src/main/resources' but your file is actually inside a subdirectory of resources then you need to provide relative path otherwise you may receive exception like-在这种情况下,默认情况下它会尝试在“src/main/resources”中查找“skyscrapper.properties”文件,但您的文件实际上位于资源的子目录中,那么您需要提供相对路径,否则您可能会收到异常,例如 -

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name src\main\resources\prop\xyz\skyscrapper.

Check project build path.检查项目构建路径。 May be only the *.java files are included.可能只包含 *.java 文件。 This problem occured in my Maven project.这个问题出现在我的 Maven 项目中。 I needed to alter my pom.xml file.我需要更改我的 pom.xml 文件。

<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
</build>

if you are using maven try this:如果您使用的是 maven 试试这个:

public static Connection getConnection(){
    //*******************************************************************
    try(InputStream input = new FileInputStream("path to file")){
        Properties prop = new Properties();
        prop.load(input);
        String driver = prop.getProperty("driver");
        String url = prop.getProperty("url");
        String user = prop.getProperty("user");
        String password = prop.getProperty("password");
        //Class.forName(driver);
        connection = DriverManager.getConnection(url, user, password);
    } catch (Exception e) {
        e.printStackTrace();

    }

    return connection;

暂无
暂无

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

相关问题 java.util.MissingResourceException:找不到基本名称消息的bundle,locale en_US - java.util.MissingResourceException: Can't find bundle for base name messages, locale en_US 原因:java.util.MissingResourceException:找不到基本名称为LocalStrings,语言环境为en_US的捆绑软件 - Caused by: java.util.MissingResourceException: Can't find bundle for base name LocalStrings, locale en_US java.util.MissingResourceException:找不到基本名称消息包,语言环境en_US Floreant POS - java.util.MissingResourceException: Can't find bundle for base name messages, locale en_US Floreant POS java.util.MissingResourceException:找不到基本名称为com.sun.org.apache.xerces.internal.impl.msg.SAXMessages的包,语言环境为en_US - java.util.MissingResourceException: Can't find bundle for base name com.sun.org.apache.xerces.internal.impl.msg.SAXMessages, locale en_US Spring MQTT:java.util.MissingResourceException:找不到基本名称 org.eclipse.paho.client.mqttv3.internal.nls.logcat,区域设置 en_US 的包 - Spring MQTT: java.util.MissingResourceException: Can't find bundle for base name org.eclipse.paho.client.mqttv3.internal.nls.logcat, locale en_US java.util.MissingResourceException:找不到基本名称视图,语言环境的捆绑包 - java.util.MissingResourceException: Can't find bundle for base name views, locale en java.util.MissingResourceException:找不到基本名称为ResBundle的包,语言环境为en_GB - java.util.MissingResourceException: Can't find bundle for base name ResBundle, locale en_GB java.util.MissingResourceException:找不到基本名称配置,语言环境的捆绑包 - java.util.MissingResourceException: Can't find bundle for base name config, locale en IntelliJ:向类路径添加资源,但仍然给我“ java.util.MissingResourceException:找不到包…语言环境en_US” - IntelliJ: Adding resources to the class path, but still gives me “java.util.MissingResourceException: Can't find bundle… locale en_US” MissingResourceException:无法找到基本名称sun.util.logging.resources.logging,locale en_US的包 - MissingResourceException: Can't find bundle for base name sun.util.logging.resources.logging, locale en_US
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM