主线程中的异常 - maven 依赖

Exception in thread main - maven dependency

public class BatchLauncherSpring {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext(
                "application-context.xml");

        //////
    }
}

我在 运行 main 方法时遇到 ClassNotFoundException。 Spring 依赖项是通过 Maven 添加的,这些依赖项是在编译时解析的。那里有很多类似的问题,但是当代码打包为 jar 并在 eclipse 之外执行时,每个人似乎都有问题。当我在 eclipse 中 运行 它时,我实际上遇到了这个问题。

这是 pom 文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>com.mycompany</groupId>
        <artifactId>mybatch</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>mybatchCore</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mybatchCore</name>
    <url></url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>3.2.2.RELEASE</spring.version>
        <spring.batch.version>2.2.5.RELEASE</spring.batch.version>
    </properties>

    <dependencies>
        <dependency>
                <groupId>com.my.repo</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>com.my.repo</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>com.my.repo</groupId>
                <artifactId>spring-oxm</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>com.my.repo</groupId>
                <artifactId>spring-batch-core</artifactId>
                <version>${spring.batch.version}</version>
            </dependency>
            <dependency>
                <groupId>com.my.repo</groupId>
                <artifactId>spring-batch-infrastructure</artifactId>
                <version>${spring.batch.version}</version>
            </dependency>


    </dependencies>

</project>

堆栈跟踪

java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext at java.net.URLClassLoader.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) Exception in thread "main"

这里有什么问题?谢谢!

没有堆栈跟踪,我们只能猜测,但我首先想到的是您将它用于 spring 依赖项的 groupId:com.my.repo。那应该是 org.springframework.

我什至不确定这是如何编译的。

你需要 spring-context 依赖 快乐编码