实现自己的 jar 时获取 NoClassDefFoundError [英] Get NoClassDefFoundError when implementing own jar

查看:43
本文介绍了实现自己的 jar 时获取 NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我正在进行的另一个项目中实现我自己的 jar(库).但是当我运行程序并尝试从我构建的库中实例化一个类时,我收到了错误 NoClassDefFoundError.

I'm trying to implement my own jar (library) in another project I'm working on. But I'm getting the error NoClassDefFoundError when I run the program and try to instantiate a class from the library I built.

我尝试以不同的方式添加依赖项,使用 maven 本地 repo 或将 jar 文件直接添加为 Build Path 中的外部 jar.我在 eclipse 中没有收到任何警告/错误,我可以使用 maven 成功构建和安装应用程序.但是当我执行使用该库中的类的部分代码时,我得到 NoClassDefFoundError.

I try to add the dependency in different ways, with maven local repo or adding the jar file directly as an external jar in Build Path. I don't get any warning/error in eclipse and I can build and install the app with maven successfully. But when I execute the part of the code that use a class from that library I get NoClassDefFoundError.

我的库 groupId 是 com.mycompany.myapp,我使用的类在包 com.mycompany.myapp.business 中.我试图将类移到父包以防万一,但我总是遇到相同的错误.

My library groupId is com.mycompany.myapp, and the classes I'm using are in the package com.mycompany.myapp.business. I'm try to move the class to the parent package just in case but I'm always getting the same error.

1 - 导入 jar 时,我是否需要定义哪些类需要可用?

1 - Do I need to define which classes need to be available when importing the jar?

2- 除了 mvn clan package/install 之外,我是否需要以特定方式编译库?

2- Do I need to compile the library in an specific way apart from mvn clan package/install?

3 - 我是否需要在父项目的库中添加相同的依赖项才能使一切正常工作?

3 - Do I need to add the same dependencies I have in my library in my parent project to have everything working?

这是我的孩子 pom.xml

This is my children pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.mylibrary</groupId>
  <artifactId>mylibrary</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
        </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>commons-discovery</groupId>
        <artifactId>commons-discovery</artifactId>
        <version>0.5</version>
    </dependency>
    <dependency>
        <groupId>wsdl4j</groupId>
        <artifactId>wsdl4j</artifactId>
        <version>1.6.3</version>
    </dependency>
    <dependency>
        <groupId>jaxen</groupId>
        <artifactId>jaxen</artifactId>
        <version>1.1.6</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.rpc</groupId>
        <artifactId>javax.xml.rpc-api</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15on</artifactId>
        <version>1.47</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpkix-jdk15on</artifactId>
        <version>1.47</version>
    </dependency>
    <dependency>
        <groupId>org.apache.axis</groupId>
        <artifactId>axis</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.rpc</groupId>
        <artifactId>javax.xml.rpc-api</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.5.0-M1</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.5.0-M1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>1.5.0-M1</version>
    </dependency>
    <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.3.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
  </dependencies>
</project>

然后在我的主项目中,我通过以下方式实例化该类:

And then in my main project I instantiate the class in the following way:

Init oInit = new Init();

其中 Init 是在 com.mycompany.mylibrary.business 包中定义的公共类.

Where Init is a public class defined in com.mycompany.mylibrary.business package.

java.lang.NoClassDefFoundError: com/mycompany/mylibrary/business/Init

来自主项目的 pom.xml:

pom.xml from the main proyect:

<dependency>
      <groupId>com.mycompany.mylibrary</groupId>
      <artifactId>mylibrary</artifactId>
      <version>0.0.1-SNAPSHOT</version>
 </dependency>

在父项目中,在 maven 导入的库中,我看到我的库有一个文件夹图标,而不是另一个.

In the parent project, in the libraries imported by maven I see that my library has a folder icon instead of the one the other have.

推荐答案

您可以使用两种方式将库包含到您的项目中:

You can use the two ways to include a library into your project:

  • 将库直接包含到您的项目类路径中
  • 通过 Maven

使用 Maven 选项时,您必须确保库已良好导出到 Maven 存储库,本地或远程(例如 Nexus).在查找 pom.xml 中定义的库时,Maven 将搜索的第一个位置位于您的本地存储库(位于 ~/.m2).它将查看中央 Maven 存储库(位于 https://mvnrepository.com/repos/central).在两者之间,您可以将自己的存储库声明为 pom.xml 为

When using the Maven option, you must be sure that the library is well exported to the Maven repository, local or remote (such as Nexus). The first place Maven will search when looking for a library defined in the pom.xml is on your local repository (located at ~/.m2). The it will look into the Central Maven Repository (located at https://mvnrepository.com/repos/central). In between, you can declare your own repositories into your pom.xml as

<repositories>
    <repository>
        <id>my-repo</id>
        <name>My Maven Repository</name>
        <url>${nexus.repositories.url}</url>
    </repository>
<repositories>

如何构建要包含在您自己的本地或远程存储库中的库? 在使用 Maven 编译时,请确保包含 install 目标以及存储库定义到它的 pom.xml

How to build the library to be included into your own local or remote repository? Just be sure to include the install goal when compiling with Maven, and also the repository definition into its pom.xml

这篇关于实现自己的 jar 时获取 NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆