类未找到异常在使用Jenkins和Maven时发生

huangapple go评论79阅读模式
英文:

ClassNotFound Exception when using jenkins and maven

问题

I need your help. I'm just getting used to Java and have finished my first milestone in my private project. Now I wanted to use this milestone as an opportunity to deal with Jenkins and CI.

However, I run into problems when running the program via Maven in Jenkins. Maven always throws me a ClassNotFound exception when processing the Jenkins pipeline. But when I start the program locally in IntelliJ, it runs without problems.

As far as I can see, it can't find a POJO which I use for parsing XML using JAXB.

Why doesn't it find a class when I build using Jenkins but finds everything when I work locally? The POM is the same.

This is my POM:

<groupId>groupId</groupId>
<artifactId>rss_backend</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.12</version>
        <scope>provided</scope>
    </dependency>
    <!-- Other dependencies -->
</dependencies>

<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>projects.rss_backend.MainApp</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>

And this is the error I get when using Jenkins/Maven:

The following command runs and outputs the execution of your Java
application (which Jenkins built using Maven) to the Jenkins UI.
+ java -jar target/rss_backend-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
	at projects.rss_backend.MainApp.main(MainApp.java:20)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	... 1 more

Do you have a clue what I'm doing wrong or how I can fix the problem?

英文:

I need your help. I'm just getting used to Java and have finished my first milestone in my private project. Now I wanted to use this milestone as an opportunity to deal with Jenkins and CI.

However, I run into problems when running the program via Maven in Jenkins. Maven always throws me a ClassNotFound exception when processing the Jenkins pipeline. But when I start the program locally in IntelliJ it runs without problems.

As far as I can see he can't find a POJO which I use for parsing XML using JAXB.

Why doesn't it find a class when I build using Jenkins but finds everything when I work locally, the POM is the same.

This is my POM:

&lt;groupId&gt;groupId&lt;/groupId&gt;
&lt;artifactId&gt;rss_backend&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

&lt;properties&gt;
    &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
    &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
&lt;/properties&gt;

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
        &lt;artifactId&gt;lombok&lt;/artifactId&gt;
        &lt;version&gt;1.18.12&lt;/version&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
        &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
        &lt;version&gt;2.2.11&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
        &lt;artifactId&gt;jaxb-impl&lt;/artifactId&gt;
        &lt;version&gt;2.2.11&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
        &lt;artifactId&gt;jaxb-core&lt;/artifactId&gt;
        &lt;version&gt;2.2.11&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;javax.activation&lt;/groupId&gt;
        &lt;artifactId&gt;activation&lt;/artifactId&gt;
        &lt;version&gt;1.1.1&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;dev.morphia.morphia&lt;/groupId&gt;
        &lt;artifactId&gt;core&lt;/artifactId&gt;
        &lt;version&gt;1.5.8&lt;/version&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;!-- Build an executable JAR --&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.0.2&lt;/version&gt;
            &lt;configuration&gt;
                &lt;archive&gt;
                    &lt;manifest&gt;
                        &lt;mainClass&gt;projects.rss_backend.MainApp&lt;/mainClass&gt;
                    &lt;/manifest&gt;
                &lt;/archive&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

</project>

and this is the error i get when using jenkins/maven:

The following command runs and outputs the execution of your Java
application (which Jenkins built using Maven) to the Jenkins UI.
+ java -jar target/rss_backend-1.0-SNAPSHOT.jar
Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
	at projects.rss_backend.MainApp.main(MainApp.java:20)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	... 1 more```

Do you have a clue what I'm doing wrong or how I can fix the problem?

答案1

得分: 1

正常的 JAR 文件不包含依赖项。

你需要按照这里的描述构建一个可执行的 JAR 文件:https://stackoverflow.com/q/574594/927493

很可能你没有通过在 IntelliJ 中使用 java -jar 启动 JAR 文件,而是使用了一些 IntelliJ 的机制为你执行了魔法(即类路径)。

英文:

Normal jars do not contain dependencies.

You need to build an executable jar as described here: https://stackoverflow.com/q/574594/927493

Probably you did not start the jar from IntelliJ with java -jar but with some IntelliJ mechanism that did the magic (i.e. the classpath) for you.

答案2

得分: 0

我建议升级您的jaxb依赖项:以下依赖在JDK11+上适用:

<!-- language:language-xml -->
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.4.0-b180830.0438</version>
</dependency>

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.4.0-b180830.0359</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.4.0-b180830.0438</version>
</dependency>
英文:

I would suggest to upgrade your jaxb deps: The following are working for me in JDK11+:

<!-- language:language-xml -->

&lt;dependency&gt;
    &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
    &lt;artifactId&gt;jaxb-impl&lt;/artifactId&gt;
    &lt;version&gt;2.4.0-b180830.0438&lt;/version&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
    &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
    &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
    &lt;version&gt;2.4.0-b180830.0359&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.glassfish.jaxb&lt;/groupId&gt;
    &lt;artifactId&gt;jaxb-runtime&lt;/artifactId&gt;
    &lt;version&gt;2.4.0-b180830.0438&lt;/version&gt;
&lt;/dependency&gt;

答案3

得分: -1

根据我所看到的,您正在使用Java 11,这意味着模块存在。您是否声明了自己的模块(在源文件夹中创建了一个module-info.java文件)?在我看过的大多数Java 8以上版本的情况下,出现ClassNotFoundException,通常是由于缺少模块依赖关系。这意味着您无法与您要尝试完成的任务进行交互。

我对Jenkins不太熟悉,但我假设可能有以下情况之一:

  • 您的项目需要来自Jenkins的某些内容
    或者
  • Jenkins尝试通过反射与您的项目中的某些内容进行交互(例如,从数据库构建对象)。

在第一种情况下,您在module-info.java中将编写:
requires(传递性)您要添加的jenkins模块 ;
在第二种情况下,您将编写:
opens 您的数据包 to 需要访问的jenkins模块 ;

在我看来,这是您收到ClassNotFoundException的最有可能的原因。无论如何,Maven也有可能是问题的根源。

英文:

As far as I can see, you are using Java 11, which means modules exist.
Did you declare your own module (create a module-info.java in your source folder)?
Most cases above Java 8 where I have seen a ClassNotFoundException, there was a module dependency missing. And that means that you cannot interact with whatever you are trying to do.

I am not familiar with Jenkins, however I assume that either

  • your project requires something from Jenkins
    or
  • Jenkins tries to interact with something from your project via Reflection (e.g. building an object from a database).

In the first case, in your module-info.java, you would write
requires (transitive) jenkins-module-you-want-to-add ;
in the second case you would write
opens your-data-package to jenkins-module-that-needs-access ;

That is in my opinion the most likely source of the ClassNotFoundException you are receiving.
Anyhow, there is also the chance that maven is the source of your problem.

huangapple
  • 本文由 发表于 2020年4月5日 20:14:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/61042411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定