Home > Software engineering >  maven project java -jar : NoClassDefFoundError: javax/mail/MessagingException
maven project java -jar : NoClassDefFoundError: javax/mail/MessagingException

Time:01-13

I have a java project that sends email built with maven (using Eclipse) and it works within Eclipse with no problem, but when trying to package a runnable jar, I am having an issue

Error: Unable to initialize main class com.my.package.MyClass
Caused by: java.lang.NoClassDefFoundError: javax/mail/MessagingException

I have this for my dependency:

    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.6.2</version>

    </dependency>

and plugins set up like so:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.0.2</version>
  <configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <mainClass>com.mypackage.MyClass</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>3.1.1</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <transformers>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>com.mypackage.MyClass</mainClass>
            <manifestEntries>
              <Multi-Release>true</Multi-Release>
            </manifestEntries>
          </transformer>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
        </transformers>
        <filters>
          <filter>
            <artifact>*:*</artifact>
            <excludes>
              <exclude>META-INF/*.SF</exclude>
              <exclude>META-INF/*.DSA</exclude>
              <exclude>META-INF/*.RSA</exclude>
            </excludes>
          </filter>
        </filters>
      </configuration>
    </execution>
  </executions>
</plugin>

I can see javax.mail-1.6.2.jar in my maven dependencies, so not sure why I am unable to run the jar...is there a config option I missed?

thanks!

I searched around and tried numerous variations of the shade and jar plugins, I expected to be able to run the jar off the command line using java -jar myjar.jar. Most of the posts I found in my search recommended adding the mail jar to the classpath, but that looks like it's already done, as the jar is in the list of maven dependencies.

Update

Have tried multiple variations, as per suggestions below. Tried the maven-assembly-plugin making sure to add the <mainClass>com.etcetera.MyClass</mainClass> tag. With those variations I got no main manifest attribute, in myjar.jar. I then added in the maven-jar-plugin plugin, also adding the main class and I'm now back to the original error.

Update2

when I open up the archive, I see this:

Manifest-Version: 1.0
Created-By: Apache Maven 3.8.4
Built-By: myUserName
Build-Jdk: 15

which appears to be missing the Main-Class: com.javabyexamples.java.jar.HelloWorld line

CodePudding user response:

When building a web app, dependencies are, of course, bundled in the .WAR/.EAR.

However, by default, Maven will not bundle dependencies inside your .jar file. (In other words, jars will not be put inside of other jars).

Nevertheless, you can use the jar-with-dependencies descriptor. More details can be found on the questions below:

Including dependencies in a jar with Maven

How can I create an executable/runnable JAR with dependencies using Maven?


Alternatively, you could add the mail-1.6.2.jar on the Java classpath like so:

java -jar "myjar.jar" -cp "./path/to/mail-1.6.2.jar:./path/to/other.jar"

Please note that classpath separators are different on Linux (colon) vs Windows (semi-colon)

CodePudding user response:

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>SendMailApp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SendMailApp</name>
  <description>SendMailApp</description>
  <packaging>jar</packaging>
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>com.sun.mail</groupId>
      <artifactId>javax.mail</artifactId>
      <version>1.6.2</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>org.example.SendMailApp</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

package

mvn clean package

target folder

target
├── activation-1.1.jar
├── javax.mail-1.6.2.jar
└── SendMailApp-0.0.1-SNAPSHOT.jar

Run

cd target
java -jar SendMailApp-0.0.1-SNAPSHOT.jar

SendMailApp-0.0.1-SNAPSHOT.jar/META-INF/MANIFEST.MF

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.8.6
Built-By: demo
Build-Jdk: 17.0.5
Class-Path: javax.mail-1.6.2.jar activation-1.1.jar
Main-Class: org.example.SendMailApp

REFERENCE - How can I create an executable/runnable JAR with dependencies using Maven?

How can I create an executable/runnable JAR with dependencies using Maven?

Answer from @Peter Mortensen and @André Aronsen

  • Related