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

后端 未结 9 1845
失恋的感觉
失恋的感觉 2020-12-09 07:30

I am trying to create a utility class ReadPropertyUtil.java for reading data from property file. While my class is located under a util directory , my sky

相关标签:
9条回答
  • 2020-12-09 08:23

    I'd like to share my experience of using Ant in building projects, *.properties files should be copied explicitly. This is because Ant will not compile *.properties files into the build working directory by default (javac just ignore *.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. Without the 'copy' section the jar file will not contain the properties files, then you may encounter the java.util.MissingResourceException.

    0 讨论(0)
  • 2020-12-09 08:28

    Try with the fully qualified name for the resource:

    private static final String FILENAME = "resources/skyscrapper";
    
    0 讨论(0)
  • 2020-12-09 08:30

    The simplest code would be like, keep your properties files into resources folder, either in src/main/resource or in 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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题