【问题标题】:NoClassDefFoundError when running application on linux (but not on mac)在 linux 上运行应用程序时出现 NoClassDefFoundError(但不在 mac 上)
【发布时间】:2017-04-19 18:17:41
【问题描述】:

我创建了一个应用程序,用于自动下载天气数据并将特定位置的这些数据添加到数据库中。此应用程序在 IntelliJ 中按预期工作

现在我想将此应用程序作为独立应用程序运行。因此,我使用 maven 创建了一个 jar。该 jar 是一个没有所有依赖项的“精简”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 服务器上运行它时(在压缩和解压缩并运行 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

通常是类路径问题,但为什么它在 macOS 和 linux 上的行为会有所不同?主 jar 文件(带有包含类路径的清单)和 lib 目录都是相同的。

【问题讨论】:

  • 对我来说,您的 maven 依赖项中似乎没有 nl.wur.fbr.om.factory.InstanceFactory。也许你是在 mac 上手动添加的?
  • 考虑使用maven-shade-plugin 来构建“完整”JAR。这通常是更好的选择。
  • @XtremeBaumer 包含该库的 jar 文件位于 lib 目录中。我从来没有手动将它添加到我的 mac。它当然在我的 Maven 存储库中。但我在运行独立应用程序时不使用 maven。

标签: java linux macos maven classnotfoundexception


【解决方案1】:
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.

【讨论】:

  • 你的步骤我试过了,现在好像是OM库的问题。显然包含InstanceFactory 的库是lib/om-java-model-0.6.0-SNAPSHOT.jar,而清单中的库lib/OM-java-model-0.6.0-SNAPSHOT.jar。注意大写。为什么 maven 会在库中不包含大写字母的情况下使用大写字母编写清单?
  • 这就是问题所在。在 OM 库(也在我的控制之下)中,大小写混淆了。我已经编辑了该库中的 pom 文件,现在引用设置正确。
猜你喜欢
  • 2016-04-29
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 2019-03-12
  • 2013-09-21
相关资源
最近更新 更多