1

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>

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?

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
DevBear
  • 13
  • 4

3 Answers3

1

Normal jars do not contain dependencies.

You need to build an executable jar as described here: How can I create an executable JAR with dependencies using Maven?

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.

Naman
  • 27,789
  • 26
  • 218
  • 353
J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
0

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

<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>
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
-1

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.

delvh
  • 39
  • 2