[JAVA] Getting Started with Creating Resource Bundles with ListResoueceBundle

Introduction

While studying java Gold, I wanted to understand the externalization of information and various properties files using sample code, so I wrote and executed the code in an integrated development environment.

However, no matter how much I try, I get a MissingResoueceException.

Why….

As a result of trial and error while staring at the run-time error, I was able to know the points to be noted. The following is a memorandum.

Sample code

MyResource.java


package com.example.Locale.properties;

import java.util.ListResourceBundle;

public class MyResource extends ListResourceBundle{

	private final static String resources[][] = {
	        {"helloworld", "Hello, World!"},
	        {"key","This is MyResource's key!"}
	    };
	@Override
	protected Object[][] getContents() {
		return resources;
	}
}

I would like to get the "key" described in the above code in the execution class.

Also, since ListResourceBundle is an abstract class, it is necessary to implement the abstract method "getContents".

Precautions in execution class

  1. Be aware of where the class files are located

For example, suppose the following sample code is shown.

ResourceBundle rb = ResourceBundle.getBundle("MyResource");

However, I couldn't get it well with this.

As a result of investigating for a small hour, the black book used for studying ([Thorough capture Java SE 8 Gold problem collection](https://www.amazon.co.jp/%E5%BE%B9%E5%BA%95] % E6% 94% BB% E7% 95% A5-Java-Gold-% E5% 95% 8F% E9% A1% 8C% E9% 9B% 86-1Z0-809 / dp / 4295000035 / ref = asc_df_4295000035 /? = jpo-22 & linkCode = df0 & hvadid = 288845875882 & hvpos = 1o1 & hvnetw = g & hvrand = 1789597220371198337 & hvpone = & hvptwo = & hvqmt = / hvdev = c & hvdvcmdl = & hvlocint = & hvlocphy = 1028851 & hvtargid = It was.

[Caution] If the resource bundle class is ** included in the package **, don't forget to qualify it with the package name.

That is, the name to be described in getBundle had to be a fully qualified name including the package name.

In the code above, the MyResource class had to be in the default package. However, since it was actually defined in the package, you need to move the file or write it in parentheses with a fully qualified name.

Below, a part of the execution class described with the fully qualified name and the execution result are listed.

--Execution class

Main.java


//Describe the resource class name as a fully qualified name.
		ResourceBundle rb = ResourceBundle.getBundle("com.example.Locale.properties.MyResource");
		String message = rb.getString("key");
		System.out.println(message);
This is MyResource's key!

Afterword

Since I am not actually working in the field due to mental and physical problems, the content may be incorrect or old-fashioned, but if I notice or find out in the future, I will write it down as a memorandum I would like to come.

I would be grateful if you could comment on any actual experiences or suggestions.

Thank you for reading.

Recommended Posts

Getting Started with Creating Resource Bundles with ListResoueceBundle
Getting Started with DBUnit
Getting Started with Ruby
Getting Started with Swift
Getting Started with Docker
Getting Started with Doma-Transactions
Getting Started with Doma-Annotation Processing
Getting Started with Java Collection
Getting Started with JSP & Servlet
Getting Started with Java Basics
Getting Started with Spring Boot
Getting Started with Ruby Modules
Getting started with Java and creating an AsciiDoc editor with JavaFX
Getting Started with Java_Chapter 5_Practice Exercises 5_4
[Google Cloud] Getting Started with Docker
Getting started with Java lambda expressions
Getting Started with Docker with VS Code
Getting Started with Doma-Criteria API Cheat Sheet
Getting Started with Ruby for Java Engineers
Getting Started with Docker for Mac (Installation)
Getting Started with Parameterization Testing in JUnit
Getting Started with Java Starting from 0 Part 1
Getting Started with Ratpack (4)-Routing & Static Content
Getting Started with Java_Chapter 8_About Instances and Classes
Links & memos for getting started with Java (for myself)
Getting Started with Doma-Using Projection with the Criteira API
Getting Started with Doma-Using Subqueries with the Criteria API
Getting Started with Java 1 Putting together similar things
Getting started with Kotlin to send to Java developers
Getting Started with Doma-Using Joins with the Criteira API
Getting Started with Doma-Introduction to the Criteria API
I tried Getting Started with Gradle on Heroku
Getting started with Java programs using Visual Studio Code
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Get started with Gradle
Proceed with Rust official documentation on Docker container (1. Getting started)
Getting Started with Doma-Dynamicly construct WHERE clauses with the Criteria API
Getting Started with Reactive Streams and the JDK 9 Flow API
Getting Started with GitHub Container Registry instead of Docker Hub