Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get a NullPointerException when initializing Spring

I've got a problem running a batch job on my server, whereas it runs fine from Eclipse on my development workstation.

I've got my Spring environment set up using Roo, made an entity, and make a batch that does some work, and test it well on my develompent box. I initialize my context and do the work, but when I run my batch on the server, the context isn't initialized properly. Here's the code:

public class TestBatch {

    private static ApplicationContext context;


    @SuppressWarnings("unchecked")
    public static void main(final String[] args) {

            context = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext.xml");
            try {
                @SuppressWarnings("unused")
                TestBatch app = new TestBatch();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    }

    public void TestBatch() { /** Do Something using the context **/ }

}

And here's the log and exception:

2010-02-16 11:54:16,072 [main] INFO  org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6037fb1e: startup date [Tue Feb 16 11:54:16 CET 2010]; root of context hierarchy
Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:194)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:127)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:458)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:388)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at tld.mydomain.myproject.batch.TestBatch.main(TestBatch.java:51)
Caused by: java.lang.NullPointerException
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:103)
    ... 7 more

Any idea or hints as to what's going on? My classpath is set to $PROJECTHOME/target/classes, and all my dependencies are in $PROJECTHOME/target/lib, and I execute using "export CLASSPATH=$PROJECTHOME/target/classes; java -Djava.endorsed.dirs=$PROJECTHOME/target/lib tld.mydomain.myproject.batch.TestBatch"

Is there anything in my setup that looks very wrong? When I run this from Eclipse, no problems, but when I deploy it on the server where I want to run it and run it as described above, I get this problem. Because it runs from Eclipse, I believe my config files are all right, but how can I debug what's causing this? Perhaps I have some config errors or a mismatch between the server and the development workstation after all? Or is this a really weird way of saying file not found, and if so, how do I make sure it finds the correct file??

I'm really looking forward to hearing your suggestions as to how to tackle this problem.

Cheers

Nik

like image 885
niklassaers Avatar asked Dec 29 '22 02:12

niklassaers


1 Answers

The cause of problem is -Djava.endorsed.dirs=$PROJECTHOME/target/lib org.springframework.beans.factory.support.DefaultListableBeanFactory contains the following code:

static {
    ClassLoader cl = DefaultListableBeanFactory.class.getClassLoader();
    try {
        javaxInjectProviderClass = cl.loadClass("javax.inject.Provider"); //Line 103
    }
    catch (ClassNotFoundException ex) {
        // JSR-330 API not available - Provider interface simply not supported then.
    }
}

It causes a NullPointerException, because getClassLoader() returns null when class is loaded via -Djava.endorsed.dirs. From javadoc:

Some implementations may use null to represent the bootstrap class loader.

So, use -classpath (with explicit specification of all jars) instead of -Djava.endorsed.dirs

like image 133
axtavt Avatar answered Jan 13 '23 11:01

axtavt