Announcement Announcement Module
Collapse
No announcement yet.
java.io.FileNotFoundException: class path resource cannot be resolved to URL ... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • java.io.FileNotFoundException: class path resource cannot be resolved to URL ...

    Hi,

    I've got project Web1 with -config.xml file:

    Code:
    <bean id="statisticResource"  class="org.springframework.core.io.FileSystemResource">
             <constructor-arg value="file-in-myjar.xml"/>
        </bean>
    file-in-myjar.xml - another -config.xml file whithin jar whithin Web1's lib directory:

    Web1
    |-WEB-INF
    |-config.xml
    |-lib
    |-myjar.jar
    |-file-in-myjar.xml

    But - I've got error:
    java.io.FileNotFoundException: class path resource [file-in-myjar.xml]
    cannot be resolved to URL because it does not exist

    Thank you.
    Last edited by zhyr; Aug 19th, 2010, 10:34 AM.

  • #2
    Try specifying the value as classpath:file-in-myjar.xml if it's in the root of the jar. If it's in a package, then try classpath:some/resource/path/file-in-myjar.xml where some/resource/path would be the package some.resource.path. You'll also want to set the class of the bean to "org.springframework.core.io.ClassPathResource " instead.
    Last edited by pgrimard; Aug 19th, 2010, 10:22 AM.

    Comment


    • #3
      I've corrected structure in first post.
      And done as you advised - file-in-myjar.xml is directly in myjar.jar:

      Code:
      <bean id="statisticResource"  class="org.springframework.core.io.FileSystemResource">
               <constructor-arg value="classpath:file-in-myjar.xml"/>
          </bean>
      New error:
      file:/C:/Servers/glassfish/domains/domain1/config/classpath:file-in-myjar.xml

      Comment


      • #4
        You need to change the bean class to "org.springframework.core.io.ClassPathResource "

        Comment


        • #5
          Code:
          <bean id="statisticResource"  class="org.springframework.core.io.ClassPathResource">
                   <constructor-arg value="classpath:file-in-myjar.xml"/>
              </bean>
          :-(
          java.io.FileNotFoundException: class path resource [classpath:file-in-myjar.xml] cannot be resolved to URL because it does not exist

          Comment


          • #6
            I just read the documentation again and ClassPathResource won't work for a resource that's in a jar file. It does say though that you can use org.springframework.core.io.UrlResource as the bean type. Leave the constructor-arg value as classpath:file-in-myjar.xml too.

            Comment


            • #7
              Thank you very much! It helped.

              Comment


              • #8
                You might actually need to specify the value as classpath*:file-in-myjar.xml.

                Comment


                • #9
                  Hi I have a similar problem here is my spring configuration :

                  <bean id="statisticResource" class="org.springframework.core.io.UrlResource">
                  <constructor-arg value="classpath:/hip/HipDataLoad.xsd"/>
                  </bean>

                  But I get this error message :

                  Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'rsloader' defined in class path resource [spring-app-resource.xml]: Cannot resolve reference to bean 'statisticResource' while setting bean property 'urlResource'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'statisticResource' defined in class path resource [spring-app-resource.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.core.io.UrlResource]: Constructor threw exception; nested exception is java.net.MalformedURLException: unknown protocol: classpath
                  Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'statisticResource' defined in class path resource [spring-app-resource.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.core.io.UrlResource]: Constructor threw exception; nested exception is java.net.MalformedURLException: unknown protocol: classpath
                  Caused by: org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.core.io.UrlResource]: Constructor threw exception; nested exception is java.net.MalformedURLException: unknown protocol: classpath
                  Caused by: java.net.MalformedURLException: unknown protocol: classpath
                  at java.net.URL.<init>(URL.java:574)
                  at java.net.URL.<init>(URL.java:464)
                  at java.net.URL.<init>(URL.java:413)
                  at org.springframework.core.io.UrlResource.<init>(Url Resource.java:69)
                  at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
                  at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
                  at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
                  at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
                  at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:85)
                  at org.springframework.beans.factory.support.SimpleIn stantiationStrategy.instantiate(SimpleInstantiatio nStrategy.java:87)
                  at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:170)
                  at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:773)
                  at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:716)
                  at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:386)
                  at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 51)
                  at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:144)
                  at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:248)
                  at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:160)
                  at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:261)
                  at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:109)
                  at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1073)
                  at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:835)
                  at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:423)
                  at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 51)
                  at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:144)
                  at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:248)
                  at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:160)
                  at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:279)
                  at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:360)
                  at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:91)
                  at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:75)
                  at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:65)
                  at com.beans.ResourceLoaderTest.main(ResourceLoaderTe st.java:47)

                  Appreciate your help. Thanks

                  Comment


                  • #10
                    You need to use a url for org.springframework.core.io.UrlResource.Check the javadoc of the org.springframework.core.io.UrlResource

                    Resource implementation for java.net.URL locators. Obviously supports resolution as URL, and also as File in case of the "file:" protocol.
                    You need to use the org.springframework.core.io.ClassPathResource

                    Resource implementation for class path resources. Uses either a given ClassLoader or a given Class for loading resources.

                    Supports resolution as java.io.File if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL.

                    Comment

                    Working...
                    X