当我用maven打包代码然后用以下代码启动时,我收到了ClassNotFoundException异常:
java -jar target\factorify-1.0-SNAPSHOT.jar
我可以在我的"胖罐"是包含所有依赖项的文件夹lib,我可以手动找到抛出ClassNotFoundException的类。
清单:
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运行该类时,它可以工作,但是当我从命令行运行它时会抛出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.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.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$1.run(Unknown Source)
at java.net.URLClassLoader$1.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
我可以在日志文件中看到这一行,而jar(activejdbc-1.4.10-SNAPSHOT.jar)与org.javalite.activejdbc.Model在那里。 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!/]
答案 0 :(得分:0)
我发现了这个问题。问题出在番石榴反射中。我用它来查找所有实体,但 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;
}