启动 Spring 启动应用程序时出现 ClassNotFoundException

ClassNotFoundException when starting Spring Boot app

当我用 Maven 打包我的代码然后启动它时,我得到 ClassNotFoundException 异常:

    java -jar target\factorify-1.0-SNAPSHOT.jar

我可以看到在我的 "fat jar" 中是包含所有依赖项的文件夹 lib,我可以在那里手动找到抛出 ClassNotFoundException 的 class。

清单:

    Manifest-Version: 1.0
    Archiver-Version: Plexus Archiver
    Built-By: Jakub
    Start-Class: me.factorify.server.Application
    Spring-Boot-Version: 1.2.0.RELEASE
    Created-By: Apache Maven 3.1.1
    Build-Jdk: 1.8.0_25
    Main-Class: org.springframework.boot.loader.JarLauncher

pom.xml:

<packaging>jar</packaging>

<properties>
    <org.springframework.version>4.1.3.RELEASE</org.springframework.version>
    <spring.boot.version>1.2.0.RELEASE</spring.boot.version>
    <org.javalite.activejdbc.version>1.4.10-SNAPSHOT</org.javalite.activejdbc.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.0.RELEASE</version>
</parent>
        ...
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring.boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

当我从我的 IDE 运行 class 时它工作,但是当我从命令行 运行 它时它抛出 ClassNotFoundException 。

知道是什么原因造成的吗?

谢谢

编辑

整个异常:

    Caused by: java.lang.NoClassDefFoundError: org/javalite/activejdbc/Model
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access0(Unknown Source)
    at java.net.URLClassLoader.run(Unknown Source)
    at java.net.URLClassLoader.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at com.google.common.reflect.ClassPath$ClassInfo.load(ClassPath.java:266)
    at me.factorify.server.common.Utils.getAllEntities(Utils.java:29)
    at me.factorify.server.config.JacksonMappingContext.<init>(JacksonMappingContext.java:29)
    at me.factorify.server.config.JacksonMappingContext$$EnhancerBySpringCGLIB$$accc13cb.<init>(<generated>)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    ... 107 more
    Caused by: java.lang.ClassNotFoundException: org.javalite.activejdbc.Model
    at java.net.URLClassLoader.run(Unknown Source)
    at java.net.URLClassLoader.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 128 more

编辑 2

我可以在日志文件中看到这一行,并且那里有带有 org.javalite.activejdbc.Model 的 jar(activejdbc-1.4.10-SNAPSHOT.jar)。 JarLauncher 一定是因为某种原因无法加载它。

 2015-01-16 01:24:04.790  INFO 7316 --- [           main] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-boot-starter-web-1.2.0.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-boot-starter-1.2.0.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-boot-1.2.0.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-boot-autoconfigure-1.2.0.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-boot-starter-logging-1.2.0.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/jcl-over-slf4j-1.7.7.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/jul-to-slf4j-1.7.7.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/log4j-over-slf4j-1.7.7.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/logback-classic-1.1.2.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/logback-core-1.1.2.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/snakeyaml-1.14.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-boot-starter-tomcat-1.2.0.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/tomcat-embed-core-8.0.15.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/tomcat-embed-el-8.0.15.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/tomcat-embed-logging-juli-8.0.15.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/tomcat-embed-websocket-8.0.15.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/jackson-databind-2.4.4.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/jackson-annotations-2.4.0.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/jackson-core-2.4.4.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/hibernate-validator-5.1.3.Final.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/validation-api-1.1.0.Final.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/jboss-logging-3.1.3.GA.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/classmate-1.0.0.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-web-4.1.3.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-context-4.1.3.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-webmvc-4.1.3.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-expression-4.1.3.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/guava-18.0.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/persistence-api-1.0.2.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-test-4.1.3.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-jdbc-4.1.3.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-beans-4.1.3.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-tx-4.1.3.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-aop-4.1.3.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/aopalliance-1.0.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-aspects-4.1.3.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/aspectjweaver-1.8.4.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/spring-core-4.1.3.RELEASE.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/commons-logging-1.2.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/jsr250-api-1.0.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/jbcrypt-0.3m.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/jsr305-3.0.0.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/activejdbc-1.4.10-SNAPSHOT.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/slf4j-api-1.7.5.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/javalite-common-1.4.10-SNAPSHOT.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/postgresql-9.1-901-1.jdbc4.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/commons-dbcp-1.4.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/commons-pool-1.5.4.jar!/, jar:file:/D:/Projekty/Factorify/target/factorify-1.0-SNAPSHOT.jar!/lib/activejdbc-instrumentation-1.4.10-SNAPSHOT.jar!/]

我发现了问题。问题出在番石榴反射中。我用它来查找所有实体,但 classInfo.load() 为 "fat jar" 抛出 NoClassDefFoundError。

我是这样修复的:(查看新行和原始行)

public static Set<Class<? extends Model>> getAllEntities() throws ClassNotFoundException {

    Set<Class<? extends Model>> allEntities = new HashSet<>();
    Set<ClassPath.ClassInfo> classes;
    try {
        classes = ClassPath.from(Application.class.getClassLoader()).getTopLevelClassesRecursive("me.factorify.server");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    for(ClassPath.ClassInfo classInfo : classes) {
        // NEW LINE
        Class<?> clazz = Application.class.getClassLoader().loadClass(classInfo.getName());

        //ORIGINAL LINE Class<?> clazz = classInfo.load();

        if (Model.class.isAssignableFrom(clazz)){
            allEntities.add((Class<? extends Model>) clazz);
        }
    }
    return allEntities;
}