0

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

Resolution

I resolved the issue by building the project twice in Eclipse: clean package and then clean compile assembly:single

1 Answers1

0

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"

It is not necessary to create an executable JAR. You can package a regular jar, allow Maven to generate the MANIFEST.MF, making sure to add the regular JAR to the Java classpath, and specify the class with the main function on the command line:

java -cp "./path/to/mail-1.6.2.jar:./path/to/other.jar:/path/to/myjar.jar" "com.mypackage.MyClass" "param1 param2 etc"

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

JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49