netbeans 异常错误:javax/激活/数据处理程序

ffscu2ro  于 2022-11-10  发布在  Java
关注(0)|答案(1)|浏览(153)

我正在用javafx创建一个聊天应用程序,并尝试使用javamail api来验证用户的电子邮件。问题是,当我运行该应用程序时,它显示了以下消息:java.lang.NoClassDefFoundError:我使用的是java 17,javafx 17,netbeans ide 12.0。
这是我写的代码:

package chat;

import java.util.Properties;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Chat extends Application{
   private Parent raiz;
   private Scene scene;
   private Stage stg1;
   private Image imagenx;

   @Override
   public void start(Stage stage) throws Exception {
      raiz = FXMLLoader.load(getClass().getResource("Chat.fxml"));
      scene = new Scene(raiz);
      scene.setUserAgentStylesheet(getClass().getResource("chat.css").toExternalForm());
      stg1 = stage;
      stg1.setScene(scene);
      imagenx = new Image(getClass().getResource("icono.jpg").toString());
      stg1.getIcons().add(imagenx);
      stg1.setTitle("Video chat");
      stg1.setWidth(800);
      stg1.setHeight(600);
      stg1.show();
   }

   private void enviarMail(String destinatario){
      String remitente = "mi remitente";
      String asunto = "enviar correo de confirmacion";
      String cuerpo = "mensaje a enviar";
      String clv = "1234";

      Properties props = System.getProperties();
      props.put("mail.smtp.host", "smtp.gmail.com");
      props.put("mail.smtp.user", remitente);
      props.put("mail.smtp.clave", clv);
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.port", "587");

      Autenticador aut = new Autenticador(remitente,clv);
      sesion = Session.getInstance(props,aut);
      mm = new MimeMessage(sesion);

      try{
        mm.setFrom(new InternetAddress(remitente));
        mm.addRecipients(Message.RecipientType.TO, destinatario);
        mm.setSubject(asunto);
        mm.setText(cuerpo);
        try(Transport transport = sesion.getTransport("smtp")) {
            transport.connect("smtp.gmail.com", remitente, clv);
            try{
                transport.sendMessage(mm, mm.getAllRecipients());
            }
            catch(MessagingException ex){
                System.out.println(ex);
            }
        }
        catch(Exception ex){
            System.out.println(ex);
        }
      }
      catch(MessagingException ex){
        System.out.println(ex);
      }
   }

   public static void main(String[] args) {
      launch(args);
      enviarMail("example1@gmail.com");
   }
}

class Autenticador extends Authenticator{
   private String smtp_usuario = "";
   private String smtp_pass = "";

   Autenticador() {}

   Autenticador(String user , String pass) {
     this.smtp_usuario = user;
     this.smtp_pass = pass;
   }

   @Override
   public PasswordAuthentication getPasswordAuthentication() {
     return new PasswordAuthentication(this.smtp_usuario, 
     this.smtp_pass);
   }
}

错误是在以下代码行中生成的:mm =新Mime消息(会话);
将库从javax.mail更改为jakarta.mail,将netbeans更新为版本12. 6,我安装了版本java 17,添加jar jakarta.activation,但当我在netbeans中运行项目时,会生成以下错误:java.lang.NoClassDefFoundError:我发现我应该添加以下依赖项:

<dependency>
  <groupId>jakarta.activation</groupId>
  <artifactId>jakarta.activation-api</artifactId>
  <version>2.0.1</version>
</dependency>

查询,我应该在哪里添加pom.xml文件和依赖项,以及如何使用jakarta.activation jar?我是自学成才的,所以这个问题可能非常明显。除了JavaMail,还有其他库可以用来发送电子邮件吗?

soat7uwm

soat7uwm1#

  • Include the Jakarta Activation library*

Jakarta Mail is the modern packaging for Java mail.
See the Jakarta Mail readme :
Jakarta Mail requires Jakarta Activation 2.0.0 or newer.
Jakarta Activation website and download .
If you use a build tool (highly recommended), such as Maven or Gradle, it will likely already add a transitive dependency on Jakarta activation to your build when you add the Jakarta mail dependency. If it doesn’t, then add the dependency yourself.

  • If you have additional issues*

Use recent versions of all software, JDK/JavaFX 17.0.2+, JakartaMail 2.0.1+. See:

  • JakartaMail release downloads.

Also, see jakarta mail compatability notes
In this release the name of that module is changed to "jakarta.mail".
So use jakarta.mail in your module-info.Java when requiring the module
OR
Place jakarta.mail in your --add-module switch, if adding modules via command line switches instead of module-info.java .

Example App

The following app compiles and runs but will fail when trying to send mail due to incorrect authentication, however, I think it demonstrates how to fix the issues with class resolution asked in the question.
Things it does:

  1. requires jakarta.mail in the module-info.
  2. Adds a dependency on jakarta mail in the pom.xml.
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>jakarta.mail</artifactId>
    <version>2.0.1</version>
</dependency>
  1. An explicit dependency on the jakarta activation framework library is not needed as it brought it via a transitive dependency on jakarta.mail.
  2. Changes the imports to use the jakarta.mail package instead of the java.mail package.
  3. Makes minor changes to the example from the question to allow it to compile and run.
  • module-info.java*
module com.example.maildemo {
    requires javafx.controls;
    requires jakarta.mail;

    exports com.example.maildemo;
}
  • 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>mail-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>mail-demo</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.7.1</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17.0.2</version>
        </dependency>
        <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-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • MailDemoApp.java*
package com.example.maildemo;

import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

import java.util.Properties;

public class MailDemoApp extends Application {

    @Override
    public void start(Stage stage) {
        Button sendMailButton = new Button("Send mail");
        sendMailButton.setOnAction(e -> enviarMail("example1@gmail.com"));
        stage.setScene(new Scene(sendMailButton));
        stage.show();
    }

    private void enviarMail(String destinatario) {
        String remitente = "127.0.0.1";
        String asunto = "enviar correo de confirmacion";
        String cuerpo = "mensaje a enviar";
        String clv = "1234";

        Properties props = System.getProperties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.user", remitente);
        props.put("mail.smtp.clave", clv);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.port", "587");

        MyAuthenticator aut = new MyAuthenticator(remitente, clv);
        Session session = Session.getInstance(props, aut);
        MimeMessage mm = new MimeMessage(session);

        try {
            mm.setFrom(new InternetAddress(remitente));
            mm.addRecipients(Message.RecipientType.TO, destinatario);
            mm.setSubject(asunto);
            mm.setText(cuerpo);

            try (Transport transport = session.getTransport("smtp")) {
                transport.connect("smtp.gmail.com", remitente, clv);
                transport.sendMessage(mm, mm.getAllRecipients());
            }
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

class MyAuthenticator extends Authenticator {
    private String smtp_usuario = "";
    private String smtp_pass = "";

    MyAuthenticator(String user, String pass) {
        this.smtp_usuario = user;
        this.smtp_pass = pass;
    }

    @Override
    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(this.smtp_usuario,
                this.smtp_pass);
    }
}
  • Maven dependency tree*
[INFO] --- maven-dependency-plugin:3.2.0:tree (default-cli) @ mail-demo ---
[INFO] com.example:mail-demo:jar:1.0-SNAPSHOT
[INFO] +- org.openjfx:javafx-controls:jar:17.0.2:compile
[INFO] |  +- org.openjfx:javafx-controls:jar:win:17.0.2:compile
[INFO] |  \- org.openjfx:javafx-graphics:jar:17.0.2:compile
[INFO] |     +- org.openjfx:javafx-graphics:jar:win:17.0.2:compile
[INFO] |     \- org.openjfx:javafx-base:jar:17.0.2:compile
[INFO] |        \- org.openjfx:javafx-base:jar:win:17.0.2:compile
[INFO] \- com.sun.mail:jakarta.mail:jar:2.0.1:compile
[INFO]    \- com.sun.activation:jakarta.activation:jar:2.0.1:compile

相关问题