{"id":308,"date":"2013-09-12T22:43:01","date_gmt":"2013-09-12T14:43:01","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=308"},"modified":"2018-09-04T13:06:11","modified_gmt":"2018-09-04T05:06:11","slug":"how-to-read-values-from-a-properties-file-located-within-a-java-jar-file","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-read-values-from-a-properties-file-located-within-a-java-jar-file\/","title":{"rendered":"How to read values from a properties file located within a Java jar file"},"content":{"rendered":"<p>A good programming practice will be to code applications to read from a text file for values which are expected to change often. Java Swing applications are often packed in a single jar file and it can make deployment easier if our Java Swing applications can read from text files embedded within the same jar file that they reside in.  <\/p>\n<p>There came a time where I needed to code a Java applet to read i18n labels from some properties files. This post documents my proof of concept before I embarked in coding that Java applet. <\/p>\n<h2>The sample scenario<\/h2>\n<p>I proved this concept with a hello world applet. This hello world applet reads a message from a properties file contained within the same jar file that it resides in. <\/p>\n<h2>Files within my jar file<\/h2>\n<p>There are just two files in my jar file:<\/p>\n<ul>\n<li>HelloWorldApplet.class<\/li>\n<li>msg.properties<\/li>\n<\/ul>\n<p>And they reside in the same directory within the jar file.<\/p>\n<p><code>HelloWorldApplet.class<\/code> is the binary of my applet, whereas <code>msg.properties<\/code> contains the message to display. <\/p>\n<p>The msg.properties file contains the following entry:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nmsg.helloworld=Hello world from jar file!\r\n<\/pre>\n<p>The message \"Hello world from jar file!\" should be accessible by my <code>HelloWorldApplet<\/code> with the \"msg.helloworld\" key.<\/p>\n<h2>Codes to realise HelloWorldApplet.class<\/h2>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.awt.Dimension;\r\nimport java.awt.event.ActionEvent;\r\nimport java.awt.event.ActionListener;\r\nimport java.io.IOException;\r\nimport java.util.ResourceBundle;\r\n\r\nimport javax.swing.JApplet;\r\nimport javax.swing.JButton;\r\nimport javax.swing.JOptionPane;\r\nimport javax.swing.JPanel;\r\nimport javax.swing.SwingUtilities;\r\n\r\npublic class HelloWorldApplet extends JApplet {\r\n\r\n    private JButton showMessageButton;\r\n\r\n    public void init() {\r\n\tSwingUtilities.invokeLater(new Runnable() {\r\n\t    public void run() {\r\n\t\tinitApplet();\r\n\t    }\r\n\t});\r\n    }\r\n\r\n    private void initApplet() {\r\n\r\n\tJPanel mainPane = new JPanel();\r\n\r\n\tshowMessageButton = new JButton(&quot;Show message&quot;);\r\n\tshowMessageButton.addActionListener( new ActionListener() {\r\n\t    public void actionPerformed(ActionEvent event) {\r\n\t\tJOptionPane.showMessageDialog(\r\n\t\t\tHelloWorldApplet.this,\r\n                        ResourceBundle.getBundle(&quot;msg&quot;).getString(&quot;msg.helloworld&quot;),\r\n\t\t\t&quot;Hello World Applet&quot;,\r\n\t\t\tJOptionPane.INFORMATION_MESSAGE\r\n\t\t);\r\n\t    }\r\n\t});\r\n\r\n\tmainPane.add(showMessageButton);\r\n\r\n\tsetContentPane(mainPane);\r\n\r\n    }\r\n\r\n}\r\n<\/pre>\n<p>I started off with overriding the <code>init<\/code> method of my Java applet class to call the <code>initApplet<\/code> method. This will ensure that my Java applet is initialized with the event dispatcher thread.<\/p>\n<p>Within the <code>initApplet<\/code> method, I build a <code>javax.swing.JPanel<\/code> to hold a <code>javax.swing.JButton<\/code> which is named as \"Show message\". <\/p>\n<p>I then create and add a <code>java.awt.event.ActionListener<\/code> to the <code>JButton<\/code>. Within the <code>actionPerformed<\/code> method, I call the <code>JOptionPane.showMessageDialog<\/code> method to display the message. <\/p>\n<p>Using <code>java.util.ResourceBundle.getBundle(\"msg\")<\/code>, I get the <code>ResourceBundle<\/code> object that gave my applet code access to the <code>msg.properties<\/code> file within the jar file. I then use the <code>getString<\/code> method of the <code>ResourceBundle<\/code> object to read the message to display with the \"msg.helloworld\" key. <\/p>\n<p>Lastly I set the JPanel object as the main panel of my applet class. <\/p>\n<h2>Why I got a MissingResourceException exception initially<\/h2>\n<p>I was being thrown the following exception trace in my face when I try to read the hello world message:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nException in thread &quot;AWT-EventQueue-2&quot; java.util.MissingResourceException: Can't find bundle for base name msg.properties, locale en_SG\r\n\tat java.util.ResourceBundle.throwMissingResourceException(Unknown Source)\r\n\tat java.util.ResourceBundle.getBundleImpl(Unknown Source)\r\n\tat java.util.ResourceBundle.getBundle(Unknown Source)\r\n\tat HelloWorldApplet$2.actionPerformed(HelloWorldApplet.java:37)\r\n\tat javax.swing.AbstractButton.fireActionPerformed(Unknown Source)\r\n\tat javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)\r\n\tat javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)\r\n\tat javax.swing.DefaultButtonModel.setPressed(Unknown Source)\r\n\tat javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)\r\n\tat java.awt.Component.processMouseEvent(Unknown Source)\r\n\tat javax.swing.JComponent.processMouseEvent(Unknown Source)\r\n\tat java.awt.Component.processEvent(Unknown Source)\r\n\tat java.awt.Container.processEvent(Unknown Source)\r\n\tat java.awt.Component.dispatchEventImpl(Unknown Source)\r\n\tat java.awt.Container.dispatchEventImpl(Unknown Source)\r\n\tat java.awt.Component.dispatchEvent(Unknown Source)\r\n\tat java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)\r\n\tat java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)\r\n\tat java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)\r\n\tat java.awt.Container.dispatchEventImpl(Unknown Source)\r\n\tat java.awt.Component.dispatchEvent(Unknown Source)\r\n\tat java.awt.EventQueue.dispatchEventImpl(Unknown Source)\r\n\tat java.awt.EventQueue.access$200(Unknown Source)\r\n\tat java.awt.EventQueue$3.run(Unknown Source)\r\n\tat java.awt.EventQueue$3.run(Unknown Source)\r\n\tat java.security.AccessController.doPrivileged(Native Method)\r\n\tat java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)\r\n\tat java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)\r\n\tat java.awt.EventQueue$4.run(Unknown Source)\r\n\tat java.awt.EventQueue$4.run(Unknown Source)\r\n\tat java.security.AccessController.doPrivileged(Native Method)\r\n\tat java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)\r\n\tat java.awt.EventQueue.dispatchEvent(Unknown Source)\r\n\tat java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)\r\n\tat java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)\r\n\tat java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)\r\n\tat java.awt.EventDispatchThread.pumpEvents(Unknown Source)\r\n\tat java.awt.EventDispatchThread.pumpEvents(Unknown Source)\r\n\tat java.awt.EventDispatchThread.run(Unknown Source)\r\n<\/pre>\n<p>It turned out to be that I had supplied \"msg.properties\" as the parameter value to the <code>ResourceBundle.getBundle<\/code> method initially. Note that we <strong>must exclude the <code>.properties<\/code> extension<\/strong> when we want to get a <code>ResourceBundle<\/code> object to read from a properties file.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-4Y\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-4Y&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-4Y&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F308&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>A good programming practice will be to code applications to read from a text file for values which are expected to change often. Java Swing applications are often packed in a single jar file and it can make deployment easier if our Java Swing applications can read from text files embedded within the same jar file that they reside in.  <\/p>\n<p>There came a time where I need to code a Java applet to read i18n labels from some properties files. This post documents my proof of concept before I embark in coding that Java applet. <\/p>\n","protected":false},"author":1,"featured_media":1220,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[6,162,168,167,169],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Java-logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-4Y","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/308"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=308"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/308\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1220"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}