Loading a config file

  • P. Simon Tuffs

    P. Simon Tuffs - 2006-02-16

    Here is my answer to a common question posed by Reinhard Gadola:

    > helo Simon
    > I'v seen your one-jar and it's really good. But I have a problem.I use
    > in the main-jar a config-File. This main-jar I've placed in the main
    > folder in the jar. Now comes the problem:
    > the main-programm cannot find that config-file....it looks like the
    > problem about the jar-file in the jar-file. how is it possible to load
    > in the main-jar that config-file.
    > is it possible to load that file with your one-jar???

    Gadola Reinhard (gadolrei) wrote:

    Hi Reihnard:

    There are two ways to open a configuration file.  (1) this.getClass().getResourceAsStream("/path-to-file-in-main.jar") and use a FileInputStrea, or (2) if you have to have a file in the filesystem then you can put the file into a directory called "files", and name that directory in the "One-Jar-Expand" property in the one-jar.jar file.

    Hope this solves your problem.

    • Josh Brumfield

      Josh Brumfield - 2008-06-26

      I have to have an external file (outside of the one-jar) on the filesystem, to allow the user to modify.  I tried your suggestion #2, and it fails with "java.util.MissingResourceException: Can't find bundle for base name".  Here is a detailed description of what I did:

      1)  Created a "files" directory in the one-jar, and put the property file in it:

      36174 Wed Jul 04 20:48:00 CDT 2007 lib/commons-cli-1.1.jar
      1192211 Fri Mar 30 00:37:00 CDT 2007 lib/svnkit.jar
      146788 Fri Mar 30 00:37:00 CDT 2007 lib/svnkit-cli.jar
      86178 Thu Jun 26 09:52:08 CDT 2008 lib/SymphonyCMWS.jar
      17363 Wed Apr 23 13:51:00 CDT 2008 lib/SymPull.jar
      1030050 Thu May 01 12:39:00 CDT 2008 lib/webserviceclient.jar
      17363 Wed Apr 23 13:51:00 CDT 2008 main/main.jar
         197 Thu Jun 26 10:37:34 CDT 2008 META-INF/MANIFEST.MF
         462 Thu Jun 26 10:39:30 CDT 2008 .classpath
           0 Mon Jun 23 14:29:42 CDT 2008 com/simontuffs/
           0 Mon Jun 23 14:32:30 CDT 2008 com/simontuffs/onejar/
        1710 Sun Sep 16 19:32:00 CDT 2007 com/simontuffs/onejar/Boot$1.class
        1915 Sun Sep 16 19:32:00 CDT 2007 com/simontuffs/onejar/Boot$2.class
        1418 Sun Sep 16 19:32:00 CDT 2007 com/simontuffs/onejar/Boot$3.class
      16249 Sun Sep 16 19:32:00 CDT 2007 com/simontuffs/onejar/Boot.class
        1454 Sun Sep 16 19:32:00 CDT 2007 com/simontuffs/onejar/Handler$1.class
         863 Sun Sep 16 19:32:00 CDT 2007 com/simontuffs/onejar/Handler.class
         259 Sun Sep 16 19:32:00 CDT 2007 com/simontuffs/onejar/IProperties.class
         868 Sun Sep 16 19:32:00 CDT 2007 com/simontuffs/onejar/JarClassLoader$1.class
         974 Sun Sep 16 19:32:00 CDT 2007 com/simontuffs/onejar/JarClassLoader$ByteCode.class
      25169 Sun Sep 16 19:32:00 CDT 2007 com/simontuffs/onejar/JarClassLoader.class
         189 Thu Jun 26 10:55:48 CDT 2008 files/sympull.properties

      2)  I included "files" in the MANIFEST:

      Manifest-Version: 1.0
      Ant-Version: Apache Ant 1.7.0
      Created-By: Symphony
      Main-Class: com.simontuffs.onejar.Boot
      One-Jar-Main-Class: SymPull
      One-Jar-Expand: files
      One-Jar-Show-Expand: true

      3)  I included files in the .classpath:

      <?xml version="1.0" encoding="UTF-8"?>
          <classpathentry kind="src" path="src"/>
          <classpathentry kind="src" path="sdk"/>
          <classpathentry kind="src" path="lib"/>
              <classpathentry kind="src" path="."/>
              <classpathentry kind="src" path=".."/>
              <classpathentry kind="src" path="files"/>
          <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
          <classpathentry kind="output" path="bin"/>

      4)  Here is the output:

      prompt$>java -jar symone.jar -propfile sympull
      Expanding to: /home/itg/Encryption/SYMONE/.
      Expanding:  files/sympull.properties
      Exception in thread "main" java.lang.reflect.InvocationTargetException
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:324)
              at com.simontuffs.onejar.Boot.run(Boot.java:306)
              at com.simontuffs.onejar.Boot.main(Boot.java:159)
      Caused by: java.util.MissingResourceException: Can't find bundle for base name sympull, locale en
              at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:838)
              at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:807)
              at java.util.ResourceBundle.getBundle(ResourceBundle.java:551)
              at SymPull.main(SymPull.java:154)
              ... 6 more

      Just to clarify, I was able to get the embedded version of the properties file to be read, by putting it in the top level directory of the one-jar (not in files).  However, I don't need the embedded one read... I need the one on the filesystem read, so that users can modify with their own properties.

      Thanks for any help,

    • Josh Brumfield

      Josh Brumfield - 2008-07-16

      Figured it out.  To help other people, I'll include my solution (I'm sure there are others).

      Instead of using PropertyResourceBundle.getBundle (as I was above), I used FileInputStream, and passed in a parameter of the full path to the property file.

      FileInputStream in = null;
      Properties props = null;
      try {
          in = new FileInputStream( myPropertyFile );
          props = new Properties();
      } catch (IOException e) {
          Util.writeLog("Exception opening property file: " + e );
      } finally {
          try {
          } catch (IOException i) {
              Util.writeLog("Exception closing property file: " + i );

      Maybe simplistic, but it works.



Log in to post a comment.