簡體   English   中英

如何修復外部 JAR 類的 NoClassDefFoundError?

[英]How to fix NoClassDefFoundError for external JAR's Classes?

我在主項目的 pom.xml 中添加了自己的外部 jar。 這是;

    <dependency>
        <groupId>com.company</groupId>
        <artifactId>antlr-plsql</artifactId>
        <scope>system</scope>
        <version>1.0</version>
        <systemPath>${project.basedir}\lib\antlr-plsql-1.0.0.jar</systemPath>
    </dependency>

當我在 eclipse 上運行時,該項目正在運行。 但是當我得到 jar 和 maven (參數:全新安裝 -DskipTests)。 我有一個錯誤;

java.lang.NoClassDefFoundError: company/antlr/core/SimplePlsqlListener
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:92)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

這個 Class 來自我的外部 jar。 外接 jar 有什么問題?

當您 jar 無法在類路徑中找到依賴項 jars 時,會導致 NoClassDefFoundError。 By default, Maven doesn't bundle dependencies in the JAR file when it builds the output jar, for the same reason you will need to provide them on the classpath when you're trying to execute your JAR file from the command-line.

這就是 JVM 在嘗試執行代碼時找不到庫 class 文件的原因。

您可以使用 -cp 參數手動指定類路徑上的庫,但這很快就會變得令人厭煩。

更好的解決方案是將庫代碼“隱藏”(包含)到您的 output JAR 文件中。 有一個名為maven-shade-plugin的 Maven 插件可以執行此操作。 您需要在 POM 中注冊它,當您運行mvn package命令時,它會自動構建一個“uber-JAR”,也稱為 FAT jar,其中包含您的類和庫代碼的類。

要簡單地捆綁所有必需的庫,請將以下內容添加到您的 POM:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.4</version>
        <configuration>
          <!-- put your configurations here -->
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

完成后,您可以重新運行上面使用的命令:

$ mvn package

您可以 在此處找到有關 maven-shade-plugin 的更多詳細信息。

此外,與其直接從其位置引用外部 jar,不如讓 maven 為您處理。 Maven 允許您使用mvn install:install-file第三方 jars

要在本地存儲庫中安裝 JAR,請使用以下命令:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

如果還有 pom 文件,您可以使用以下命令安裝它:

mvn install:install-file -Dfile=<path-to-file> -DpomFile=<path-to-pomfile>

With version 2.5 of the maven-install-plugin, it can get even simpler: if the JAR was built by Apache Maven, it'll contain a pom.xml in a subfolder of the META-INF/ directory, which will be read by默認。 在這種情況下,您需要做的就是:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=<path-to-file>

這對我有用; 首先運行這個命令:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=<path-to-file>

之后,將 pom.xml 依賴項更改為:

    <dependency>
        <groupId>com.company</groupId>
        <artifactId>antlr-plsql</artifactId>
        <scope>compile</scope>
        <version>1.0</version>
    </dependency>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM