Eclipse: java.lang.NoClassDefFoundError: javax/activation/DataHandler

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48750800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:32:16  来源:igfitidea点击:

Eclipse: java.lang.NoClassDefFoundError: javax/activation/DataHandler

javaeclipseemail

提问by gabboSonc

I'm trying to send an email in java. Here's the code:

我正在尝试用 Java 发送电子邮件。这是代码:

String mailSmtpHost = "smtp.example.com";
String mailTo = "[email protected]";
        String mailFrom = "[email protected]";
        String mailSubject = "Email subject";
        String mailText = "Some text";
        Properties properties = new Properties();
        properties.put("mail.smtp.host", mailSmtpHost);
        Session emailSession = Session.getDefaultInstance(properties);
try {
            Message emailMessage = new MimeMessage(emailSession);
            emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
            emailMessage.setFrom(new InternetAddress(mailFrom));
            emailMessage.setSubject(mailSubject);
            emailMessage.setText(mailText);
            emailSession.setDebug(true);
            Transport.send(emailMessage);
        }catch(Exception e) {
            System.out.println("Errore email: "+e.toString());
        }

When I compile the project, on line:

当我编译项目时,在线:

Message emailMessage = new MimeMessage(emailSession);

...the following error comes out:

...出现以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataHandler

Note: I'm using java -version: 9.0.4

注意:我正在使用 java -version: 9.0.4

回答by Léo R.

JDK 9 disables access to many of the javax.* APIs by default, javax activation is now deprecated.

默认情况下,JDK 9 禁用对许多 javax.* API 的访问,现在不推荐使用 javax 激活。

However you can resolve it with adding the module at run time

但是,您可以通过在运行时添加模块来解决它

"--add-modules java.activation".

回答by Grzesiek Kapuscinski

download and add external jakarta.activation-1.2.1.jar Its the Jakarta Activation project.

下载并添加外部 jakarta.activation-1.2.1.jar 这是 Jakarta Activation 项目。