Home > Enterprise >  java.lang.NoClassDefFoundError: javax/activation/DataSource exception when I try to send an email in
java.lang.NoClassDefFoundError: javax/activation/DataSource exception when I try to send an email in

Time:11-24

I'm trying to create an Java Swing (with Ant) application to send an email to a desired address(My first time).The application builds successfully but when I click the button to send the email I get a lot of different error messages, with the first being a java.lang.NoClassDefFoundError: javax/activation/DataSource .The sender email is new and I did not assosciate it with a phone number(I was told it might lead to issues). I've also have already added the mail.jar to the project library. The catch JOptionPane did not print any errors as well so I'm having a hard time figuring this out. I've attached the code as well as a screenshot of the error.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
    String toEmail= "email1";//Changed emails for securityPurposes

    String fromEmail = "email2";
    String fromEmailPassword="Password";
    String subject="This is the subject";

    Properties properties = new Properties();
    properties.put("mail.smtp.auth","true");
    properties.put("mail.smtp.starttls.enable","true");
    properties.put("mail.smtp.host","smtp.gmail.com");
    properties.put("mail.smtp.port","587");
    
    Session session=Session.getDefaultInstance(properties,new >javax.mail.Authenticator(){
    @Override
    protected PasswordAuthentication getPasswordAuthentication(){
    return new PasswordAuthentication(fromEmail,fromEmailPassword);
    }
    });
    try{
        MimeMessage message=new MimeMessage(session);
        message.setFrom(new InternetAddress(fromEmail));
        message.addRecipient(Message.RecipientType.TO,new >InternetAddress(toEmail));
        message.setSubject(subject);
        message.setText("Hello");
        Transport.send(message);
    }
    catch(Exception ex){
        JOptionPane.showMessageDialog(null, ex);
    }

    
}                                        

I've looked but haven't found any similar cases or solutions.

CodePudding user response:

In Java 11, all of the so-called "enterprise" modules got removed from the JDK and JRE. That includes APIs like JAXB, but also the Java Activation Framework. That's needed by JavaMail, but your application is missing it (as shown by the error message).

Add this dependency: https://mvnrepository.com/artifact/jakarta.activation/jakarta.activation-api/1.2.2. Make sure to stay away from version 2.x and higher, those have a different package name.

CodePudding user response:

java.lang.NoClassDefFoundError means the class can' t be found in the classpath, based on your description I think you are creating a jar by ant and then execute it with java -jar. You should check:

  1. in the java command there is a -cp option with the needed jar
  2. the needed jar is correctly pointed at
  •  Tags:  
  • java
  • Related