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