Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: problem of reading the config file from a jar file...

  1. #1
    Join Date
    Mar 2005
    Posts
    6

    Default 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. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    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

  3. #3
    Join Date
    Nov 2004
    Location
    St. Louis, Missouri USA
    Posts
    33

    Default

    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?

  4. #4
    Join Date
    Mar 2005
    Posts
    6

    Default 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!

  5. #5
    Join Date
    Mar 2005
    Posts
    6

    Default 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)

  6. #6
    Join Date
    Nov 2004
    Location
    St. Louis, Missouri USA
    Posts
    33

    Default

    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?

  7. #7
    Join Date
    Mar 2005
    Posts
    6

    Default 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 ]

  8. #8
    Join Date
    Nov 2004
    Location
    St. Louis, Missouri USA
    Posts
    33

    Default

    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

  9. #9
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote 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

  10. #10
    Join Date
    Mar 2005
    Posts
    16

    Default

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

Similar Threads

  1. Replies: 2
    Last Post: Apr 12th, 2012, 09:34 AM
  2. Saving large files to a db using hibernate?
    By Dan Washusen in forum Data
    Replies: 10
    Last Post: Sep 20th, 2006, 12:18 PM
  3. Replies: 2
    Last Post: Aug 27th, 2005, 08:51 AM
  4. Replies: 2
    Last Post: May 2nd, 2005, 05:33 AM
  5. Replies: 8
    Last Post: Mar 3rd, 2005, 06:25 PM

Posting Permissions

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