Announcement Announcement Module
Collapse
No announcement yet.
Accessing classpath resources from within a jar Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing classpath resources from within a jar

    Hi,
    I have developed a native application (in SWT) using spring that needs to access a properties file, as follows:

    Code:
      <bean id="placeholderConfig"      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location"><value>/myapp.properties</value></property>
      </bean>
    The "location" property expects a classpath resource, which is accessible just fine when I run the application normally (i.e., not from within a jar). I just add the directory where the properties file is stored to the classpath JVM argument.

    However, when I try to package my code into a jar file, I get:

    Exception in thread "main" org.springframework.beans.factory.BeanInitializati onException: Could not load properties from class path resource [myapp.properties]; nested exception is java.io.FileNotFoundException: Could not open class path resource [myapp.properties]
    Even though my manifest file has the directory of the properties file in its "Class-Path" entry.

    When I output the system property java.class.path, I get simply my jar file. Is it possible that spring's class resource loader is not seeing past the jar file to look into the Class-Path specified in the manifest? This has been a very sticky problem and I would appreciate any help you can offer.

  • #2
    Have you tried adding a leading slash to resource name, assuming that it is stored in the root of the JAR file?

    Rob

    Comment


    • #3
      Try this

      Code:
        <bean id="placeholderConfig"      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="location"><value>classpath&#58;/myapp.properties</value></property>
        </bean>
      Ollie

      Comment


      • #4
        Actually I can see from the original exception that Spring is already using a ClassPathResource so that shouldn't help...

        Comment


        • #5
          How about using 'classpath*' instead of 'classpath'?

          Lawrence

          Comment


          • #6
            yeah, have you tried: "classpath*:myapp.properties"

            Comment


            • #7
              I tried the classpath* idea, to no avail. Keep in mind that the resource is a user-modifiable configuration file that needs to be stored *outside* the jar file.

              So assuming the config file is stored in /usr/share/myapp/conf/myapp.properties, I would expect that I can just add either: /usr/share/myapp/conf/myapp.properties or /usr/share/myapp/conf to the Class-Path entry in the jar file's manifest and it would be able to see it.

              What is so perplexing is that the file is visible to Spring when I run the program normally (no jar file), but as soon as I package it in a jar, I get these problems. Any further ideas?

              Comment


              • #8
                ahh k... I was able to run something like that using file:/usr/share/myapp/conf/myapp.properties (not tested linux env) or in my case file:C:/usr/share/myapp/conf/myapp.properties

                Comment


                • #9
                  Ah yes that is possible to use the "file:" prefix, but that forces the path to the file in question to be *hard-wired*. Thus the install directory for my app would be immutable because my spring file would only be able to look in one place for its properties. So I really feel forced to use the classpath approach.

                  Comment


                  • #10
                    Try this

                    Code:
                    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                    		<property name="location" ref="propertyResource"></property>
                    	</bean>
                    
                    <bean name="propertyResource" class="org.springframework.core.io.ClassPathResource">
                    		<constructor-arg><value>spring-config.properties</value></constructor-arg>
                    	</bean>

                    Comment

                    Working...
                    X