Announcement Announcement Module
Collapse
No announcement yet.
problem of reading the config file from a jar file... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • problem of reading the config file from a jar file...

    I packed the spring bean factory config file into a jar file. And then I use the jar in my project. It is reported that "can not find the config file...".
    I works without jar if I unpack those into "class" dir.

    Any help will be much appreciated!

  • #2
    It Seems that you access your configuration via a filename. This cannot work within a jar. In a jar you have to locate the configuration via the classpath.

    Maybe ClassPathXmlApplicationContext can be helpful for you.

    Hope that helps,
    Andreas

    Comment


    • #3
      the usual questions: is the jar on the classpath?
      Can you show us some code where you're trying to access it? how about your build, what does the internal structure of the jar look like?

      Comment


      • #4
        thanks

        Yes, the jar is in the classpath. Because the found factory class is in my jar.

        The sample code is:
        private final URL url = this.getClass().getResource("oracle_spatial_Contex t.xml");
        // String[] files = {url.getPath()};
        String[] files = {url.getFile()};
        ac = new FileSystemXmlApplicationContext(files);

        Maybe the problem is FileSystemXmlApplicationContext. I will try to use ClassPathXmlApplicationContext.

        Thanks!

        Comment


        • #5
          still errors

          but though I try ClassPathXmlApplicationContext, it was also reported the FileNotFoundException Exception:

          I give my code here(oracle_spatial_Context.xml is in the same package as this class.):
          URL url = this.getClass().getResource("oracle_spatial_Contex t.xml");
          String[] files = {url.getPath()};
          ac = new ClassPathXmlApplicationContext(files);

          Exception:
          Caused by: org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from URL [file:/E:/LXJ/webapp02/WEB-INF/lib/cluster_oracle_spatial-1.0.jar!/com/efn/Engine/FriendChain/ClusterOracleSpatial/Util/oracle_spatial_Context.xml]; nested exception is java.io.FileNotFoundException: E:\LXJ\webapp02\WEB-INF\lib\cluster_oracle_spatial-1.0.jar!\com\efn\Engine\FriendChain\ClusterOracleS patial\Util\oracle_spatial_Context.xml
          at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:144)
          at org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:83)
          at org.springframework.context.support.AbstractXmlApp licationContext.loadBeanDefinitions(AbstractXmlApp licationContext.java:101)
          at org.springframework.context.support.AbstractXmlApp licationContext.loadBeanDefinitions(AbstractXmlApp licationContext.java:69)
          at org.springframework.context.support.AbstractRefres hableApplicationContext.refreshBeanFactory(Abstrac tRefreshableApplicationContext.java:87)
          at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:262)
          at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:80)

          Comment


          • #6
            Is there directory structure inside the Jar?
            http://www.springframework.org/docs/...hResource.html

            ClassPathXmlApplicationContext("com/efn/Engine/FriendChain/ClusterOracleSpatial/Util/oracle_spatial_Context.xml ");

            Does something like that work?

            Comment


            • #7
              yes

              yes, there is a directory structure inside the jar.
              I use your given method, but it was still error:

              Caused by: org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from class path resource [com/efn/Engine/FriendChain/ClusterOracleSpatial/Util/oracle_spatial_Context.xml ]; nested exception is java.io.FileNotFoundException: Could not open class path resource [com/efn/Engine/FriendChain/ClusterOracleSpatial/Util/oracle_spatial_Context.xml ]

              Comment


              • #8
                After digging around in the spring src a little, maybe some of these might work?
                From the ContextSingletonBeanFactoryLocator:
                Code:
                ClassPathXmlApplicationContext&#40;"classpath*&#58;oracle_spatial_Context.xml "&#41;;

                Inside AdvisorAutoProxyCreatorTests you see:
                Code:
                	/**
                	 * Return a bean factory with attributes and EnterpriseServices configured.
                	 */
                	protected BeanFactory getBeanFactory&#40;&#41; throws IOException &#123;
                		return new ClassPathXmlApplicationContext&#40;"/org/springframework/aop/framework/autoproxy/advisorAutoProxyCreator.xml"&#41;;
                	&#125;
                So maybe try it with leading slash
                Code:
                ClassPathXmlApplicationContext&#40;"/com/efn/Engine/FriendChain/ClusterOracleSpatial/Util/oracle_spatial_Context.xml"&#41;;
                And inside SingletonBeanFactoryLocatorTests you see:
                Code:
                	public void testBaseBeanFactoryDefs&#40;&#41; &#123;
                		// Just test the base BeanFactory/AppContext defs we are going to work with
                		// in other tests.
                		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext&#40;
                				"/org/springframework/beans/factory/access/beans*.xml"&#41;;
                	&#125;
                so spring's loaders look pretty flexible about know how to dig around in the classpath

                Comment


                • #9
                  Originally posted by khagel
                  so spring's loaders look pretty flexible about know how to dig around in the classpath
                  Actually it is the default behaviour of the java ClassLoader#getResourceXXX methods. So it is no speciality of spring (though efficiently instrumented ).

                  Regards,
                  Andreas

                  Comment


                  • #10
                    try to put the xml file in the root of the jar file and then use
                    Code:
                    ClassPathXmlApplicationContext&#40;"oracle_spatial_context.xml"&#41;;

                    Comment


                    • #11
                      Finally I didn't pack the config file into my jar. But it seemed that spring might still have the problem...

                      I have another questionMaybe I should start up another thread, but i think maybe they have relations...)

                      I have 3 jar file: a.jar,b.jar,c.jar. Some classes in a.jar need to reference some classes in b.jar and c.jar. I put them into the appropriate lib directory in my EJB application.(I am running jboss and I have them in $JBOSS_HOME/server/default/lib)

                      I deployed an EJB application: ejbapp.jar. One of the EJB will invoke ClassA.class of a.jar to initialize a spring bean factory. Code in ClassA.class looks like:

                      ...
                      BeanFactory myfactory = new FileSystemXmlApplicationContext("mycontext.xml");
                      ...

                      The problem comes: The BeanFactory CAN NOT be initialized! The root cause was a ClassNotFoundException: those classes in b.jar and c.jar ,defined in mycontext.xml, can not be found!

                      I tried to modify the MANIFEST file in a.jar, it looks like:
                      Manifest-Version: 1.0
                      Class-Path: b.jar c.jar

                      But it didn't take effect...

                      Comment


                      • #12
                        Are you using the BEA JRockit jvm? if so you probably have the same problem as me. It seems that the resource loading on JRockit behaves differently as on the SUN jvm. When I run my application on SUN's jdk '1.4.2_06' it works fine but when I switch to JRockit '1.4.2_05' it can't find it's bean configuration files.

                        Comment


                        • #13
                          Re: still errors

                          Originally posted by linxiaojun
                          but though I try ClassPathXmlApplicationContext, it was also reported the FileNotFoundException Exception:

                          I give my code here(oracle_spatial_Context.xml is in the same package as this class.):
                          URL url = this.getClass().getResource("oracle_spatial_Contex t.xml");
                          String[] files = {url.getPath()};
                          ac = new ClassPathXmlApplicationContext(files);

                          Exception:
                          Caused by: org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from URL [file:/E:/LXJ/webapp02/WEB-INF/lib/cluster_oracle_spatial-1.0.jar!/com/efn/Engine/FriendChain/ClusterOracleSpatial/Util/oracle_spatial_Context.xml]; nested exception is java.io.FileNotFoundException: E:\LXJ\webapp02\WEB-INF\lib\cluster_oracle_spatial-1.0.jar!\com\efn\Engine\FriendChain\ClusterOracleS patial\Util\oracle_spatial_Context.xml
                          at ...
                          linxiaojun,

                          First of all, you are feeding both FileSystemXmlApplicationContext and ClasspathXmlAppicationContext file: URLs. Now the fact of the matter is that ClasspathXmlAppicationContext can handle file: urls, but in that case it will behave just like FileSystemXmlApplicationContext!

                          And the reason FileSystemXmlApplicationContext is not working is that you are not giving it a valid file url (path). You are getting a resource from the classloader, and that resource is coming from a jar which is actually never unpacked into the filesystem. So when you ask the URL of the resource for the path with
                          url.getPath()
                          you get back a
                          file:/E:/LXJ/webapp02/WEB-INF/lib/cluster_oracle_spatial-1.0.jar!/com/efn/Engine/FriendChain/ClusterOracleSpatial/Util/oracle_spatial_Context.xml

                          this is not a legal 'file:' url string!

                          What you should do is just feed to the constructors the String returned by URL.toString(). What you should get back then is some sort of registered url type that still points into the jar file, but the url resolver in your VM knows how to handle.

                          Alternately, you can use ClasspathXmlApplicationContext, and feed it a valid package specific path to the resource, e.g.
                          com/efn/Engine/FriendChain/ClusterOracleSpatial/Util/oracle_spatial_Context.xml

                          for this variant, you can actually use ClassUtils.addResourcePathToPackagePath() to help you build the right path to the resource.

                          Regards,

                          Comment


                          • #14
                            Originally posted by linxiaojun

                            I have another questionMaybe I should start up another thread, but i think maybe they have relations...)

                            .......


                            But it didn't take effect...
                            SORRY, I made a mistake. I compile the b.jar and c.jar in JDK1.5 and run it in JRE1.4.2... It is the mistake!


                            Colin Sampaleanu, thank you for you clear explaination. I think the problem is resolved.

                            Comment


                            • #15
                              It may sound silly but remove the space that you have after the xml file. It just worked for me just now.

                              Comment

                              Working...
                              X