Results 1 to 10 of 10

Thread: Accessing classpath resources from within a jar

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

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

    Rob

  3. #3
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    335

    Default

    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

  4. #4
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    335

    Default

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

  5. #5
    Join Date
    Aug 2004
    Location
    Hong Kong
    Posts
    26

    Default

    How about using 'classpath*' instead of 'classpath'?

    Lawrence

  6. #6
    Join Date
    Oct 2004
    Posts
    3

    Default

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

  7. #7

    Default

    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?

  8. #8
    Join Date
    Oct 2004
    Posts
    3

    Default

    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

  9. #9

    Default

    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.

  10. #10
    Join Date
    May 2008
    Posts
    3

    Default

    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>

Similar Threads

  1. Replies: 9
    Last Post: Feb 15th, 2012, 12:19 PM
  2. Replies: 6
    Last Post: Dec 18th, 2007, 09:44 PM
  3. Accessing another .ear's POJOs
    By mkoss in forum Container
    Replies: 3
    Last Post: Oct 17th, 2005, 08:24 AM
  4. FactoryBean for configuring Resources.
    By rcui in forum Container
    Replies: 0
    Last Post: Apr 17th, 2005, 09:55 PM
  5. Access resources in WEB-INF
    By dev001 in forum Web
    Replies: 10
    Last Post: Nov 22nd, 2004, 03:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •