[ Back | Previous | Next ]

How to use property resource bundles in applets?

Package:
java.util.*
Product:
JDK
Release:
1.1.x
Related Links:
Calendar
EventObject
Hashtable
Locale
Properties
PropertyResourceBundle
TimeZone
Comment:
Using property resource bundles in applets
Source

Remember last month's Example3 applet -- the applet that works with property resource bundles? I mentioned that this applet would not run under Netscape because it tries to access the user's machine to read the underlying property file, and this activity results in a security violation. This security violation causes the Java runtime code to throw a MissingResourceException object. So how can we get past this problem?

The solution is to place Example3's class and property files in a jar file, and to include a reference to this jar file in the <APPLET> tag. The following code fragment uses the jar command (under Windows 95) to create a jar file called example3.jar. This command places example3.class and all files that end with a .properties extension into this jar file:


                       jar -cvf example3.jar example3.class *.properties

The following code fragment shows the <APPLET> tag with the new ARCHIVE attribute. This attribute is used to identify the jar file.
                        <APPLET ARCHIVE="example3.jar" 
                        CODEBASE="/javaworld/jw-01-1999/internationalize" 
                        CODE="example3.class" 
                        WIDTH=200 
                        HEIGHT=220>
                       </APPLET>

 

And now for the grand finale! The following PropertyResourceBundle example shows the same Example3 applet we were introduced to in last month's installment. The source code and class files are the same. The only differences are that the Example3 class and properties files have been placed into a jar file, and that the <APPLET> tag has an ARCHIVE attribute.

1