6 Replies Latest reply: Oct 6, 2010 12:44 PM by 803507 RSS

    Unable to access .properties file in Jar

    803507
      I have a jar containing useful utility classes and a default properties file to support them.

      In a class packaged into a second jar with the first on the classpath I am calling:

      MainClass.class.getResource("/utility.properties");

      However this always returns null.

      I've tried moving it off the root package, using "Thread.getCurrentThread().getContextLoader..." and System Class loaders and none of them can locate the properties file.

      I know the classpath is working as the Main class includes some classes from the utility jar file.
      I can even manually loadClass a class in the utility jar (and ironically in the same package as the properties file) and it returns the correct Jar URL.

      How do you load a properties file from a JAR in the class path?
        • 1. Re: Unable to access .properties file in Jar
          JoachimSauer
          Try AUtilityClassInTheSamePackageAsThePropertiesFile.class.getResource("utility.properties");
          • 2. Re: Unable to access .properties file in Jar
            803507
            Tried that - no luck.

            Even more bizzare:

            If I have a class like-

            package a.b.testing;

            public class TestProperties {
            public static void main(String[] args) {
            System.out.println(TestProperties.class.getResource("/a/b/testing/TestProperties.class"));
            System.out.println(TestProperties.class.getResource("test.properties"));
            }
            }

            When I run against a class path that isn't compiled into a jar then I get:
            file:/C:/.../build/classes/a/b/testing/TestProperties.class
            file:/C:/.../build/classes/a/b/testing/test.properties

            However package into a JAR with only these two classes in it.
            Then run:
            java -cp test.jar a.b.testing.TestProperties

            file:/C:/.../build/classes/a/b/testing/TestProperties.class
            null

            I have manually decompiled the JAR and confirmed that it contains the properties files

            This to me is worse than I first thought as it appears I can't even reference a .properties file from within the same JAR as the Main class???
            • 3. Re: Unable to access .properties file in Jar
              800560
              user8098060 wrote:
              When I run against a class path that isn't compiled into a jar then I get:
              file:/C:/.../build/classes/a/b/testing/TestProperties.class
              file:/C:/.../build/classes/a/b/testing/test.properties

              However package into a JAR with only these two classes in it.
              Then run:
              java -cp test.jar a.b.testing.TestProperties

              file:/C:/.../build/classes/a/b/testing/TestProperties.class
              null

              I have manually decompiled the JAR and confirmed that it contains the properties files

              This to me is worse than I first thought as it appears I can't even reference a .properties file from within the same JAR as the Main class???
              It works for me using your code. That is, instead of null I see a path. I don't think your testing of the jar file is correct. At least, it should not show "file:" anything if the resource is in a jar. It should show "jar:file:" etc.

              Your test.properties file must be jar'd so it remains in the a/b/testing path. When I "jar tf thejarfile.jar" I created I get:
              a/b/testing/test.properties
              a/b/testing/TestProperties.class
              • 4. Re: Unable to access .properties file in Jar
                baftos
                Can you copy and paste here the output of
                jar -tf test.jar
                ?
                • 5. Re: Unable to access .properties file in Jar
                  803507
                  Sorry, cut n pasted the wrong window.

                  To clarrify:

                  - the first run was against a source tree so the file: based urls are correct.
                  - The second run returns:
                  C:\Projects\>java -classpath Testing.jar a.b.testing.TestProperties
                  jar:file:/C:/Projects/Testing.jar!/a/b/testing/TestProperties.class
                  null

                  - I've manually inspected the jar and it does indeed contain
                  a/b/testing/test.properties
                  a/b/testing/TestProperties.class

                  I just don't understand how it can find the class file in the jar but not the properties???

                  I'm completey at a loss as to why it is not resolving?
                  • 6. Re: Unable to access .properties file in Jar
                    803507
                    Bugger!

                    Sorry guys, it's a typo.

                    the properties file was called "Testing.properties" not "testing.properties".

                    So it worked fine outside the Jar on a windows (case insensitive) file system but as soon as it was placed in the (case sensitive) jar it would fail.

                    Thanks for help and sorry for wasting everybodies time!