views:

193

answers:

2

I have a class that loads a springframework application context like so:

package com.offlinesupport;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class OfflineScriptSupport {

    private static ApplicationContext appCtx;

    public static final void initialize() {
        appCtx = new ClassPathXmlApplicationContext( new String[] {
                "mycontext.spring.xml"
        } );
    }

    public static final ApplicationContext getApplicationContext() {
        return appCtx;
    }

    public static final void main( String[] args ) {
        System.out.println( "Starting..." );
        initialize();
        System.out.println( "loaded" );
    }
}

The class OfflineScriptSupport, and the file mycontext.spring.xml are each deployed into separate jars (along with other classes and resources in their respective modules). Lets say the jar files are OfflineScriptSupport.jar and *MyContext.jar". mycontext.spring.xml is put at the root of the jar.

In a Jython script (*myscript.jy"), I try to call the initialize method to create the application context:

from com.offlinesupport import OfflineScriptSupport

OfflineScriptSupport.initialize();

I execute the Jython script with the following command (from Linux):

jython -Dpython.path=spring.jar:OfflineScriptSupport.jar:MyContext.jar myscript.jy

The Springframework application context cannot find the mycontext.spring.xml file. It displays the following error:

java.io.FileNotFoundException: class path resource [mycontext.spring.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:137)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:167)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:148)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:126)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:142)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:113)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:81)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:89)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:269)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:87)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:72)
    at com.offlinesupport.OfflineScriptSupport.initialize(OfflineScriptSupport.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)

If I run the jar directly from Java (using the main entry point in OfflineScriptSupport) it works and there is no error thrown.

Is there something special about the way Jython handles classpaths making the Springframework's ClassPathXmlApplicationContext not work (i.e. not be able to find resource files in the classpath)?

A: 

You may need to use "/mycontext.spring.xml" classpath resource instead to indicate a full path.

Eugene Kuleshov
Thanks for the suggestion. I tried it out but it still gives the error. The resource reference works as-is when run directly in java (using the 'main' entry point). I also tried using a groovy script and it works. I think I'll just go with groovy.
staticman
A: 

Try using FileSystemXmlApplicationContext("mycontext.spring.xml") instead.

"Plain paths will always be interpreted as relative to the current VM working directory". http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/context/support/FileSystemXmlApplicationContext.html

Milanka