Java ClassNotFoundException:org.springframework.context.support.AbstractApplicationContext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21000685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:50:54  来源:igfitidea点击:

ClassNotFoundException: org.springframework.context.support.AbstractApplicationContext

javaweb-servicesjarmainclassws-client

提问by Introspective

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

我正在尝试遵循此Apache CXF – JAX-WS – 简单教程,但构建下载的示例(开箱即用!)会创建一个在调用时拒绝运行的客户端,并发出以下错误:

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

无法从 SampleWSCxfClient-0.0.1-SNAPSHOT.jar 加载主类清单属性

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

我搜索以了解有关此问题的更多信息,并找到了这个 SO 答案,这促使我通过使用以下行打开它7-zip并将其添加到文件名中来破解 SampleWSCxfClient-0.0.1-SNAPSHOT.jar META-INF/MANIFEST.MF

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

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

我通过简单地查看项目中唯一的 Java 文件来找出主类:

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:

然后我尝试再次调用 SampleWSCxfClient-0.0.1-SNAPSHOT.jar,但这次它失败了:

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.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.

为方便起见,可以从这里下载整个示例包 ZIP 。

采纳答案by Withheld

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.

我曾经遇到过类似的问题,在无法解读从命令行运行“具有许多依赖项的 jar”的魔力之后,我通过 Maven 运行它来解决它。

Try this:

尝试这个:

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

回答by sayan

for maven based projects, please follow the below link to create runnable jar. For maven based project, making jar using export->jar option doesnt work. It work for me successfully.

对于基于 maven 的项目,请按照以下链接创建可运行的 jar。对于基于 maven 的项目,使用 export->jar 选项制作 jar 不起作用。它成功地为我工作。

CREATING RUNNABLE JAR for maven based eclipse project

为基于 Maven 的 Eclipse 项目创建 RUNNABLE JAR

回答by Vivek Sen

I had the same problem but it got fixed after I updated the classpathPrefix in my pom.xml. Support_lib/ .

我有同样的问题,但在我更新 pom.xml 中的 classpathPrefix 后它得到了修复。支持_lib/。

After you build the project using maven, the 'target' folder had this Support_lib/ dir with all the spring jar files in it. so I used this dir name in the classpathPrefix and it worked.

使用 maven 构建项目后,'target' 文件夹中有这个 Support_lib/ 目录,其中包含所有 spring jar 文件。所以我在 classpathPrefix 中使用了这个目录名称并且它起作用了。