1

i have a jar named SpringPoc.jar which i have exported using eclipse as a runnable jar

inside the jar i have 2 classes one is

public class NormalServlet {

    public void executeScheduler() {        
         ApplicationContext context = 
                 new ClassPathXmlApplicationContext("com/serv/Beans.xml");

          CallMeth obj = (CallMeth) context.getBean("callMeth");
          obj.test();       
    }}

2nd class is Main App

public class MainApp {

public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("com/serv/Beans.xml");

    CallMeth obj = (CallMeth) context.getBean("callMeth");
    obj.test();
}}

i have defined a single bean inside Beans.xml

 <bean id="callMeth" class="com.poc.CallMeth"/>

then i have created a simple java project in eclipse having a test class which calls the jar and tries to execute the above class NormalServlet

public class Test {

    public static void main(String[] args) {

        try {

            URL[] classLoaderUrls =  new URL[]{new URL("file:\\D:\\MyWorkspace\\Scheduler\\src\\com\\callit\\SpringPoc.jar")};
             URLClassLoader urlClassLoader = new URLClassLoader(classLoaderUrls);

                Class classToLoad = Class.forName ("com.serv.NormalServlet", true, urlClassLoader);
                Method method = classToLoad.getDeclaredMethod ("executeScheduler");
                Object instance = classToLoad.newInstance ();
                Object result = method.invoke (instance);   

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

when i run test class

i get the below error

Sep 09, 2016 11:48:30 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c50507: startup date [Fri Sep 09 11:48:30 IST 2016]; root of context hierarchy
Sep 09, 2016 11:48:30 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/serv/Beans.xml]
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.callit.Scheduler.callMe(Scheduler.java:40)
    at com.callit.Scheduler.main(Scheduler.java:15)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/serv/Beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/serv/Beans.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.serv.NormalServlet.executeScheduler(NormalServlet.java:17)
    ... 6 more
Caused by: java.io.FileNotFoundException: class path resource [com/serv/Beans.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
    ... 19 more

if i run the same jar from cmd as java -jar SprrinPoc.jar the MainApp class is called and the Beans.xml loads fine

Question is why doesn't it load when i call Normalservlet externally from test class

Thanks a lot

Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32
  • where is you XML file located? – Jens Sep 09 '16 at 06:50
  • @Jens xml is located under com.serv package here the NomalServlet class exists – user3375428 Sep 09 '16 at 07:27
  • Try to add a slash in front of the path – Jens Sep 09 '16 at 07:42
  • tried ApplicationContext context = new ClassPathXmlApplicationContext("/com/serv/Beans.xml"); getting exception Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/serv/Beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/serv/Beans.xml] cannot be opened because it does not exist – user3375428 Sep 09 '16 at 07:47

1 Answers1

0

As long as the dependency jar has file included xxx.xml, it should be available in your host project classpath as well. This will search applicationcontext.xml anywhere in classpath

ApplicationContext context = new ClassPathXmlApplicationContext(
            "classpath*:**/applicationContext.xml");
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • tried ApplicationContext context = new ClassPathXmlApplicationContext( "classpath*:**/beans.xml"); it throws a nobeanfound exception . but i think it doesn't load the xml . cause i modified the above code as ApplicationContext context = new ClassPathXmlApplicationContext( "classpath*:**/beans1.xml"); .note : i have no xml named beans1.xml ... still it threw a nobeanfoundexception – user3375428 Sep 09 '16 at 07:35
  • exact exception Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'callMeth' is defined – user3375428 Sep 09 '16 at 07:43
  • then is it is possibly not loaded, are you sure, your jar available in classpath? – kuhajeyan Sep 09 '16 at 07:53
  • jar is at a separate location .i am using the below code to work on the jar URL[] classLoaderUrls = new URL[]{new URL("file:\\D:\\MyWorkspace\\Scheduler\\src\\com\\callit\\SpringPoc.jar")};it is able to find the jar and invoke the method(executeScheduler) in the class NormalServlet but ApplicationContext context = new ClassPathXmlApplicationContext("com/serv/Beans.xml"); is not able to ind the Beans.xml – user3375428 Sep 09 '16 at 08:01
  • then you should be using FileSystemXmlApplicationContext – kuhajeyan Sep 09 '16 at 08:03
  • if i use FileSystemXmlApplicationContext then it tries to find the xml under D:\MyWorkspace\Scheduler\ which is wong xml is inside the jar......exception is INFO: Loading XML bean definitions from file [D:\MyWorkspace\Scheduler\com\serv\Beans.xml] Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [D:\MyWorkspace\Scheduler\com\serv\Beans.xml]; nested exception is java.io.FileNotFoundException: com\serv\Beans.xml (The system cannot find the path specified) – user3375428 Sep 09 '16 at 08:09
  • you need to load the jar file from zipinputstream. check similar questions here http://stackoverflow.com/questions/17515757/reading-files-within-a-jarzipinputstream-etc , http://stackoverflow.com/questions/3710615/reading-a-zip-file-within-a-jar-file – kuhajeyan Sep 09 '16 at 08:19