Issue
When I try to build a jar file from my Application it gives me the following exception:
Error: Unable to initialize main class de.ff.JavaMailTest.SendEmailTLS
Caused by: java.lang.NoClassDefFoundError: jakarta/mail/MessagingException.
The Class works fine if I run it in IntelliJ. Here is my Code:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven- 4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.ff</groupId>
<artifactId>JavaMailTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>de.ff.JavaMailTest.SendEmailTLS</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>```
SendEmailTSL.java:
package de.ff.JavaMailTest;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import java.util.Properties;
public class SendEmailTLS {
public static void main(String[] args) {
System.setProperty("file.encoding", "UTF-8");
final String username = args[0];
final String password = args[1];
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.web.de");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(prop,
new jakarta.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(args[2])
);
message.setSubject(args[3]);
message.setText(args[4]);
Transport.send(message);
System.out.println("Succes");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
Thanks for the help!
Solution
You need to build Jar file which specifies Main-Class
attribute.
You can create is Manually or by using Assembly plugin or using IntelliJ
- Creating runnable jar manually - https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
- Creating runnable jar using Assembly plugin - https://maven.apache.org/plugins/maven-assembly-plugin/usage.html
- Use following steps to create runnable jar using IntelliJ
Following are the steps to do it using IntellIj -
- Open Project structure dialog (File -> Project Structure)
- Select Artifacts option from Project settings
- Click + button to add
Jar
artifact and selectFrom modules with dependencies
- Select your main class in
Create JAR from modules dialog
and click OK
- Save your configuration by Clicking OK
- Click
Build Artifacts
fromBuild
menu and and SelectBuild
in submenu
- Now run the built Jar file which should run without any issues.
Answered By - Sachin Gorade