Announcement Announcement Module
Collapse
No announcement yet.
Getting applicationContext from a jar throws a FileNotFoundException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting applicationContext from a jar throws a FileNotFoundException

    Hi,
    I am trying to use Spring 2.0 within SwiftMQ. For information, I developed a swiftlet (swiftMQ specific functionnality). A swiftlet is simply codes packaged in a jar.

    My problem is that I can't get the applicationContext. I got a FileNotFoundException when trying to instanciate the ClassPathXmlApplicationContext.

    Here is the code used to load the context:
    Code:
            URL applicationContextUrl = this.getClass().getClassLoader().getResource("applicationContext.xml");        
    //        ClassPathXmlApplicationContext springContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            ClassPathXmlApplicationContext springContext = new ClassPathXmlApplicationContext(applicationContextUrl.getPath());
            service = (ServiceTraduction) springContext.getBean("serviceTraduction");
    (I also tried the code in comment)

    Can someone help me please??

    Does a property on the jar file would make Spring not found the file? The applicationContextUrl is not null.

    Regards,
    Last edited by david.chau; Dec 18th, 2006, 08:33 AM. Reason: adding precision

  • #2
    What exactly does appCtxUrl.getPath() returns? And what is the stacktrace? Is there a problem with the file which is used to create an app context or while the appCtx is created, the exception is thrown by a code which is instantiated/executed?

    Note that you can pass a Class object to indicate what Classloader should be used.

    Comment


    • #3
      What exactly does appCtxUrl.getPath() returns?
      It returns that:
      file:/D:/Programs/swiftmq_6_2_1/scripts/win32/../../deploy/router1/xt$testSwiftlet/_deployed_1166450057296/xt$testSwiftlet.jar!/applicationContext.xml

      And what is the stacktrace?
      com.swiftmq.swiftlet.SwiftletException: org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist, removing bundle.

      Is there a problem with the file which is used to create an app context or while the appCtx is created, the exception is thrown by a code which is instantiated/executed?
      Here is my mapping file:
      Code:
      ?xml version="1.0" encoding="ISO-8859-1"?>
      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
      
      <!-- Configuration Spring du composant de traduction message USG vers Pacage -->
      <beans>
      
        <bean id="serviceTraduction" class="fr.gouv.agriculture.traducteur.ServiceTraduction" init-method="init" scope="prototype">
          <property name="fichierConfiguration">
              <value>configuration-traduction.xml</value>        
          </property>
        </bean>
        
        <bean id="traducteur" class="fr.gouv.agriculture.traducteur.Traducteur">
          <constructor-arg>
              <ref local="serviceTraduction"/>
          </constructor-arg>
        </bean>
      
      </beans>

      Comment


      • #4
        Try passing your class to the constructor of the application context or use classpath* to make sure that all classpath roots are searched (please see the docs about the downsides of classpath*).

        Comment


        • #5
          I did that :
          Code:
                  ClassPathXmlApplicationContext springContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml", ServiceTraduction.class);
          and I have this stacktrace: org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from class path resource [classpath*:applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource[classpath*:applicationContext.xml] cannot be opened because it does not exist,removing bundle. Correct the error and deploy again!

          Do you think swiftMQ make something that can makes the initialisation failed?

          Comment


          • #6
            for kicks, what happens if you put it into a sub-directory in your JAR, then reference it as "classpath*:/foo/your_config.xml"

            Comment


            • #7
              It does not change anything.

              Comment


              • #8
                I'm not sure what swiftMQ does at startup but it seems that your classpath is not that obvious. I would suggest (as I do in most of this cases) to do a simple class which calls getClass().getClassLoader().getResource/getResources() and use various resources which should be available on the classpath. use also / to understand what is the root for it.
                Play with it - it should provide enough information to understand what resources are accessible and which ones are not.

                Comment


                • #9
                  Apparently, I have a similar problem when I try to load a property file.

                  Comment


                  • #10
                    Muh

                    Now i have exactly the same problem... i have

                    Code:
                    		String realPath = evt.getServletContext().getRealPath("WEB-INF/SpringDispatcher-servlet.xml");
                    		
                    		ApplicationContext ctx = new ClassPathXmlApplicationContext(realPath, ContextListener.class);
                    and in realPath i have C:/Dokumente und Einstellungen/Nico/workspace/sf/WEB-INF/SpringDispatcher-servlet.xml

                    and if i make start -> run in windows and copy exactly this in, the file opens...
                    Why is it so hard to put the simple path in???
                    Last edited by Dosihris; Dec 26th, 2006, 05:35 PM.

                    Comment


                    • #11
                      Doesn't this work?
                      Code:
                      ApplicationContext ctx = new ClassPathXmlApplicationContext("SpringDispatcher-servlet.xml");

                      Comment


                      • #12
                        hey kaldi

                        you again... cool...

                        No, unfortunately not... i get this exception




                        2006-12-26 23:50:49,453 [main] DEBUG org.apache.catalina.loader.WebappClassLoader - --> Resource not found, returning null
                        2006-12-26 23:50:52,906 [main] ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/sf] - Exception sending context initialized event to listener instance of class de.hosp.sf.config.ContextListener
                        org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from class path resource [SpringDispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: class path resource [SpringDispatcher-servlet.xml] cannot be opened because it does not exist
                        Caused by:
                        java.io.FileNotFoundException: class path resource [SpringDispatcher-servlet.xml] cannot be opened because it does not exist
                        at org.springframework.core.io.ClassPathResource.getI nputStream(ClassPathResource.java:137)
                        at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:351)
                        at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:334)
                        at org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:126)
                        at org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:142)
                        at org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:168)
                        at org.springframework.context.support.AbstractXmlApp licationContext.loadBeanDefinitions(AbstractXmlApp licationContext.java:113)
                        at org.springframework.context.support.AbstractXmlApp licationContext.loadBeanDefinitions(AbstractXmlApp licationContext.java:79)
                        at org.springframework.context.support.AbstractRefres hableApplicationContext.refreshBeanFactory(Abstrac tRefreshableApplicationContext.java:94)
                        at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:292)
                        at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:92)
                        at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:77)
                        at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:68)
                        at de.hosp.sf.config.ContextListener.contextInitializ ed(ContextListener.java:29)
                        at org.apache.catalina.core.StandardContext.listenerS tart(StandardContext.java:3763)
                        at org.apache.catalina.core.StandardContext.start(Sta ndardContext.java:4211)
                        at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1013)
                        at org.apache.catalina.core.StandardHost.start(Standa rdHost.java:718)
                        at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1013)
                        at org.apache.catalina.core.StandardEngine.start(Stan dardEngine.java:442)
                        at org.apache.catalina.core.StandardService.start(Sta ndardService.java:450)
                        at org.apache.catalina.core.StandardServer.start(Stan dardServer.java:709)
                        at org.apache.catalina.startup.Catalina.start(Catalin a.java:551)
                        at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
                        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
                        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
                        at java.lang.reflect.Method.invoke(Unknown Source)
                        at org.apache.catalina.startup.Bootstrap.start(Bootst rap.java:294)
                        at org.apache.catalina.startup.Bootstrap.main(Bootstr ap.java:432)

                        Comment


                        • #13
                          OK, take a step back. What is it your trying to do? Load a Spring applicationContext when the web server starts?

                          Comment


                          • #14
                            correct. i read the document you posted me and there they have an example

                            Code:
                            public final class Boot {
                            
                                public static void main(final String[] args) throws Exception {
                                    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml", Boot.class);
                                    FooService fooService = (FooService) ctx.getBean("fooService");
                                    fooService.insertFoo (new Foo());
                                }
                            }
                            Thats there example. And i need an ApplicationContext too, if i understood it right. so i tried to build it same as they did. And when i start the WebServer, i get this exception.

                            I cant explain it to me.. Maybe.. something is different, i'll tell you, maybe its the reason.

                            I have build a contextListenerObject (actually a friend of mine did, cause i have no idea what to do). public class ContextListener implements ServletContextListener

                            In this class i load in the function public void contextInitialized(ServletContextEvent evt) my ApplicationContext. Is there a special class or place or time where i have to do this?

                            Comment


                            • #15
                              If you just want to ensure the applicationContext is initialized by the web server you can simply use the ContextLoaderListener. If you have a look at the petclinic example that ships with Spring, the web.xml has comments to explain how this all works.

                              http://www.springframework.org/docs/...rListener.html

                              Comment

                              Working...
                              X