ClassNotFoundException: org.springframework.context.support.AbstractApplicationContext

Introspective picture Introspective · Jan 8, 2014 · Viewed 10.7k times · Source

I am trying to follow this Apache CXF – JAX-WS – Simple Tutorial but building the downloaded sample (out of the box!) creates a client that upon invocation refuses to run, issuing this error:

Failed to load Main-Class manifest attribute from SampleWSCxfClient-0.0.1-SNAPSHOT.jar

I searched to find out more about this problem and found out this SO answer which prompted me to hack SampleWSCxfClient-0.0.1-SNAPSHOT.jar by opening it using 7-zip and adding into a file name META-INF/MANIFEST.MF the following line:

Main-Class: com.areyes.sample.client.SampleWSClient

I figured out that main class by simply looking at the only Java file in the project:

package com.areyes.sample.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sample.service.SampleWebService;

public class SampleWSClient {


    public SampleWSClient() {

        ClassPathXmlApplicationContext classPathXmlAppContext = new ClassPathXmlApplicationContext("classpath:META-INF/beans.xml");
        classPathXmlAppContext.start();

        SampleWebService sampleWebService = (SampleWebService)classPathXmlAppContext.getBean("client");

        System.out.println(sampleWebService.getDataFromWebService().getName());
        System.out.println(sampleWebService.getDataFromWebService().getDescription());
        System.out.println(sampleWebService.getDataFromWebService().getAge());

    }

    public static void main(String[] args){
        new SampleWSClient();
    }
}

I then tried to invoke SampleWSCxfClient-0.0.1-SNAPSHOT.jar again, but this time it fails with:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/support/AbstractApplicationContext
Caused by: java.lang.ClassNotFoundException: org.springframework.context.support.AbstractApplicationContext
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.areyes.sample.client.SampleWSClient. Program will exit.

How do I make this sample work?


For your convenience, the entire sample package ZIP can be downloaded from here.

Answer

Withheld picture Withheld · Jan 9, 2014

I once came across a similar problem and after being unable to decipher the magic of running "a jar with many dependencies" from the command line, I worked around it by running it through Maven.

Try this:

mvn exec:java -Dexec.mainClass=com.areyes.sample.client.SampleWSClient