I ended up using ResourceBundle
when using Locate in Java.
Therefore, when the properties file for ResourceBundle
was placed in the resource directory, an error (java.util.MissingResourceException) occurred that the target file could not be found, so a memorandum for placing the puroperties file in any location is shown below. leave.
** ▼ Directory structure **
In Main.java
, I prepared Locales from Japan and the United States and placed the properties files for each locale directly under the resource directory, but I was told that the properties files could not be found.
The cause is that the classpath could not be referenced below.
C:\tool\pleiades\workspace\gold\bin
By the way, the above can be confirmed by the following description.
Main.java
class Main {
public static void main(String[] args) {
System.out.println(System.getProperty("java.class.path"));
}
As an aside, in order to read the properties file with the above classpath, it is necessary to place it directly under the main
directory.
In conclusion, use ʻURLClassLoader`.
Main.java
class Main {
public static void main(String[] args) throws MalformedURLException {
File dicDir = Paths.get(".\\resource").toFile(); // ★1
URLClassLoader urlLoader; // ★2
urlLoader = new URLClassLoader(new URL[]{dicDir.toURI().toURL()}); // ★3
Locale localeJp = Locale.JAPAN;
Locale localeUs = Locale.US;
List<Locale> locales =
new ArrayList<Locale>(Arrays.asList(localeJp, localeUs));
for (Locale locale : locales) {
ResourceBundle rb =
ResourceBundle.getBundle("Source" ,locale ,urlLoader); // ★4
System.out.println(rb.getString("apple") + rb.getString("orange"));
}
}
}
The procedure to refer to the properties file placed in an arbitrary location is as follows.
Now you can refer to the properties file in any location by changing the path of ★ 1.
Recommended Posts