Java 邮件:NoClassDefFoundError:javax/activation/DataSource

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13980209/
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-10-31 14:37:56  来源:igfitidea点击:

Java Mail: NoClassDefFoundError: javax/activation/DataSource

javaemailclasspathnoclassdeffounderrorsunos

提问by Steve

I wrote a small Java command line program to test sending emails from a remote server. I'm getting the dreaded "NoClassDefFoundError" and I can't figure out why.

我编写了一个小型 Java 命令行程序来测试从远程服务器发送电子邮件。我收到了可怕的“NoClassDefFoundError”,我不知道为什么。

The server is running:

服务器正在运行:

  • SunOS 5.10 Generic January 2005
  • Java 1.5.0_30-b03 ( Sun, standard )
  • SunOS 5.10 通用 2005 年 1 月
  • Java 1.5.0_30-b03(Sun,标准)

My java program is called

我的java程序被称为

SendEmailACME

发送电子邮件ACME

The error message is

错误信息是

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

The complete output from the run of the program is:

程序运行的完整输出是:

bash-3.00$ javac SendEmailACME.java
bash-3.00$ java SendEmailACME
SendEmailACME: Classpath: .:/users/steve/TestProgramsLib/mail.jar:users/steve/TestProgramsLib/activation.jar
DEBUG: setDebug: JavaMail version 1.4.4
Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataSource
        at SendEmailACME.main(SendEmailACME.java:47)
bash-3.00$ 

I ran

我跑了

java -verbose SendEmailACME

java -verbose SendEmailACME

The ouput was too long for stackoverflow. All it included was the regular output, plus a bunch of messages about java loading all of its regular libraries, the libraries from mail.jar, but I didn't see any from javax.activation.*

输出对于 stackoverflow 来说太长了。它包含的只是常规输出,以及一堆关于 java 加载其所有常规库(mail.jar 中的库)的消息,但我没有看到 javax.activation.* 中的任何内容。

Output from "$ echo $CLASSPATH" is:

“$ echo $CLASSPATH”的输出是:

bash-3.00$ echo $CLASSPATH
.:/users/steve/TestProgramsLib/mail.jar:users/steve/TestProgramsLib/activation.jar
bash-3.00$

My home directory is

我的主目录是

/users/steve

It contains these two directories

它包含这两个目录

  1. TestPrograms
  2. TestProgramsLib
  1. 测试程序
  2. 测试程序库

The first has my program SendEmailACME.java, SendEmailACME.class/ The second has the following jars in it:

第一个有我的程序 SendEmailACME.java, SendEmailACME.class/ 第二个有以下 jars:

bash-3.00$ ls -l
total 1102
-rw-r--r--   1 steve  acme      55932 Apr 19  2006 activation.jar
-rw-r--r--   1 steve  acme     494975 Jan 14  2011 mail.jar
bash-3.00$

This is the source code of my command line program SendEmailACME:

这是我的命令行程序 SendEmailACME 的源代码:

import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

import java.util.Properties;

public class SendEmailACME {


    public static void main(String[] args) throws Exception{


        String smtpServer  = "msg.abc.acme.com";
        int port           = 25;        
        String userid      = "acme.staffdirectory"; 
        String password    = "password";  
        String contentType = "text/html";

        String subject     = "test: Send An Email, From A Java Client Using msg.abc.acme.com";
        String from        = "[email protected]";
        String to          = "[email protected],[email protected],[email protected],[email protected]";
        String body        = "<h1>Test. An Email, From A Java Client Using msg.abc.acme.com.</hi>";

        System.out.println("SendEmailACME: Classpath: " + System.getProperty("java.class.path"));

        Properties props   = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.host", smtpServer);

        Session mailSession = Session.getInstance(props);

        // Get runtime more runtime output when attempting to send an email
        mailSession.setDebug(true);

        MimeMessage message = new MimeMessage(mailSession);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, to);
        message.setSubject(subject);
        message.setContent(body,contentType);

        Transport transport = mailSession.getTransport();
        transport.connect(smtpServer, port, userid, password);
        transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
        transport.close();
    }// end function main()

}// end class SendEmailACME

Here is the output from running a command to see what is inside activation.jar:

这是运行命令以查看 activation.jar 中的内容的输出:

bash-3.00$ jar -tf activation.jar
META-INF/MANIFEST.MF
META-INF/SUN_MICR.SF
META-INF/SUN_MICR.RSA
META-INF/
META-INF/mailcap.default
META-INF/mimetypes.default
javax/
javax/activation/
javax/activation/ActivationDataFlavor.class
javax/activation/MimeType.class
javax/activation/MimeTypeParameterList.class
javax/activation/MimeTypeParseException.class
javax/activation/CommandInfo.class
javax/activation/DataHandler.class
javax/activation/DataHandler.class
javax/activation/DataSource.class
javax/activation/CommandMap.class
javax/activation/DataContentHandler.class
javax/activation/DataContentHandlerFactory.class
javax/activation/CommandObject.class
javax/activation/DataHandlerDataSource.class
javax/activation/DataSourceDataContentHandler.class
javax/activation/ObjectDataContentHandler.class
javax/activation/FileDataSource.class
javax/activation/FileTypeMap.class
javax/activation/MailcapCommandMap.class
javax/activation/MimetypesFileTypeMap.class
javax/activation/SecuritySupport.class
javax/activation/SecuritySupport.class
javax/activation/SecuritySupport.class
javax/activation/SecuritySupport.class
javax/activation/SecuritySupport.class
javax/activation/SecuritySupport.class
javax/activation/URLDataSource.class
javax/activation/UnsupportedDataTypeException.class
com/
com/sun/
com/sun/activation/
com/sun/activation/registries/
com/sun/activation/registries/MailcapFile.class
com/sun/activation/registries/MailcapParseException.class
com/sun/activation/registries/MimeTypeFile.class
com/sun/activation/registries/MimeTypeEntry.class
com/sun/activation/registries/LineTokenizer.class
com/sun/activation/registries/LogSupport.class
com/sun/activation/registries/MailcapTokenizer.class
com/sun/activation/viewers/
com/sun/activation/viewers/ImageViewer.class
com/sun/activation/viewers/ImageViewerCanvas.class
com/sun/activation/viewers/TextEditor.class
com/sun/activation/viewers/TextViewer.class
bash-3.00$

Everything compiles fine, but it can't seem to find javax.activation.DataSource despite activation.jar being in the classpath

一切都编译得很好,但它似乎无法找到 javax.activation.DataSource 尽管 activation.jar 在类路径中

I do not have access to the jdk_home/jre/lib/ext directory.

我无权访问 jdk_home/jre/lib/ext 目录。

I have been attempting to execute SendEmailACME from my directory /users/steve/TestPrograms

我一直在尝试从我的目录 /users/steve/TestPrograms 执行 SendEmailACME

Thanks in advance for any help

在此先感谢您的帮助

Steve

史蒂夫

采纳答案by Ian Roberts

bash-3.00$ echo $CLASSPATH
.:/users/steve/TestProgramsLib/mail.jar:users/steve/TestProgramsLib/activation.jar

You appear to be missing a /between mail.jar:and users/steve. This means javais looking in the wrong place for activation.jar(in ./usersrather than /users).

您似乎缺少/介于mail.jar:和之间users/steve。这意味着java在错误的地方寻找activation.jar(in./users而不是/users)。

回答by tcb

Your CLASSPATH doesn't contain JDK libraries where javax.* libraries are placed.

您的 CLASSPATH 不包含放置 javax.* 库的 JDK 库。