英文:
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:
<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>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>dev.morphia.morphia</groupId>
<artifactId>core</artifactId>
<version>1.5.8</version>
</dependency>
</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?
答案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 -->
<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>
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论