NoClassDefFoundError when 运行 application on linux (but not on mac)
NoClassDefFoundError when running application on linux (but not on mac)
我创建了一个应用程序,用于自动下载天气数据并将这些特定位置的数据添加到数据库中。此应用程序在 IntelliJ
中按预期工作
现在我想运行这个应用程序作为一个独立的应用程序。因此,我使用 Maven 创建了一个 jar。该 jar 是一个没有所有依赖项的 "lean" jar。依赖项输出到目录 lib
。 Maven 插件配置为:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>nl.wur.fbr.data.weather.WeatherData</mainClass>
</manifest>
</archive>
<outputDirectory>${project.build.directory}/WeatherData</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/WeatherData/lib</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
类路径添加到清单(我已经检查过)并且所有依赖项都输出到 lib 目录。
当我在 Mac(Java 版本 1.8.0)上 运行 这个应用程序时,一切正常。但是当我 运行 它在 linux 服务器上时(在压缩和解压缩以及 运行ning Java 1.8.0 之后),我得到以下错误:
$ java -jar weatherData-1.0-SNAPSHOT.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: nl/wur/fbr/om/factory/InstanceFactory
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: nl.wur.fbr.om.factory.InstanceFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
通常是类路径问题,但为什么它在 macosx 和 linux 上表现不同?主 jar 文件(带有包含类路径的清单)和 lib 目录都是相同的。
I am not sure what is exactly causing this issue but below steps might help to narrow down the path.
Step 1 : Verify the class file nl.wur.fbr.om.factory.InstanceFactory present inside jar
Step 2: Verify MANIFEST.MF file and check for the following entries -
ex: Main-Class: nl.wur.fbr.data.weather.WeatherData
Class-Path:
../config/
classes12.jar
bc4jct.jar
bc4jdomorcl.jar
bc4jmt.jar
commons-beanutils-bean-collections.jar
commons-beanutils-core.jar
Step 3 : Verify the jar entry is present in Class-Path
Step 4: If the issue still persists, try running with java -classpath option.
我创建了一个应用程序,用于自动下载天气数据并将这些特定位置的数据添加到数据库中。此应用程序在 IntelliJ
中按预期工作现在我想运行这个应用程序作为一个独立的应用程序。因此,我使用 Maven 创建了一个 jar。该 jar 是一个没有所有依赖项的 "lean" jar。依赖项输出到目录 lib
。 Maven 插件配置为:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>nl.wur.fbr.data.weather.WeatherData</mainClass>
</manifest>
</archive>
<outputDirectory>${project.build.directory}/WeatherData</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/WeatherData/lib</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
类路径添加到清单(我已经检查过)并且所有依赖项都输出到 lib 目录。
当我在 Mac(Java 版本 1.8.0)上 运行 这个应用程序时,一切正常。但是当我 运行 它在 linux 服务器上时(在压缩和解压缩以及 运行ning Java 1.8.0 之后),我得到以下错误:
$ java -jar weatherData-1.0-SNAPSHOT.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: nl/wur/fbr/om/factory/InstanceFactory
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: nl.wur.fbr.om.factory.InstanceFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
通常是类路径问题,但为什么它在 macosx 和 linux 上表现不同?主 jar 文件(带有包含类路径的清单)和 lib 目录都是相同的。
I am not sure what is exactly causing this issue but below steps might help to narrow down the path.
Step 1 : Verify the class file nl.wur.fbr.om.factory.InstanceFactory present inside jar
Step 2: Verify MANIFEST.MF file and check for the following entries -
ex: Main-Class: nl.wur.fbr.data.weather.WeatherData
Class-Path:
../config/
classes12.jar
bc4jct.jar
bc4jdomorcl.jar
bc4jmt.jar
commons-beanutils-bean-collections.jar
commons-beanutils-core.jar
Step 3 : Verify the jar entry is present in Class-Path
Step 4: If the issue still persists, try running with java -classpath option.