gpt4 book ai didi

Java 运行时 jar 丢失

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:27 27 4
gpt4 key购买 nike

我是java新手,

我正在尝试从我的 java 类发送电子邮件,我在 Windows 中尝试了代码,最初有一些问题。将 mail-1.4.jar 放入 C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext 后,它运行正常。

我刚刚将其移至我的服务器(AWS),将此 java 文件放入文件夹中并尝试运行此类。我将 mail-1.4.jar 放在路径 /opt/jdk1.7.0_79/jre/lib/ext

我的java类:

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {

public static void main(String[] args) throws IOException,
URISyntaxException, AddressException, MessagingException {
String BODY = "\n\n Regards Admin.";
String SUBJECT = "SUBJECT";
String TO = "fg@gddgh.com";

final String FROM = "no-reply@dfh.com";
final String SMTP_USERNAME = "SMTP_USERNAME";
final String SMTP_PASSWORD = "SMTP_PASSWORD";
final String HOST = "email-smtp.us-west-2.amazonaws.com";

String message = "Email successfully sent";

Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", 25);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");

Session session = Session.getDefaultInstance(props);

MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(FROM));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
msg.setSubject(SUBJECT);
msg.setContent(BODY, "text/plain");

Transport transport = session.getTransport();
try {
transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println(message);
} catch (Exception ex) {
message = "Email not sent to the given ID";
System.out.println(message);
} finally {
transport.close();
}

}
}

执行javac Mail.java命令时编译良好,但无法运行该类。我在执行 java Mail

时遇到以下异常
    Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Address
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
at java.lang.Class.getMethod0(Class.java:2866)
at java.lang.Class.getMethod(Class.java:1676)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: javax.mail.Address
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

最佳答案

看来您没有将 jar 包含到 openJdk 中。

尝试将 jar 添加到您的 openJdk pathToOpenJdk/jre/lib/ext 文件夹中。它应该有效。

希望这有帮助。

关于Java 运行时 jar 丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39657432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com