java.lang.NoClassDefFoundError:com / sun / mail / util / MailLogger for Java Mail的JUnit测试用例


127

我正在使用Java Mail API读取和解析电子邮件。它可以与Servlet代码一起正常工作。

现在,出于相同的目的,我试图编写一个JUnit测试用例。但是在网上 Session session = Session.getDefaultInstance(properties);我得到以下异常:

java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
    at javax.mail.Session.initLogger(Session.java:227)
    at javax.mail.Session.<init>(Session.java:212)
    at javax.mail.Session.getDefaultInstance(Session.java:315)
    at javax.mail.Session.getDefaultInstance(Session.java:355)
    at com.vxl.appanalytix.dataload.fromEmail.EmailParser.parseEmailSubject(EmailParser.java:44)
    at com.vxl.appanalytix.controllers.controllerClasses.GenericEmailServiceClass.readEmailAttachments(GenericEmailServiceClass.java:33)
    at com.vxl.appanalytix.dataload.GenericEmailTestCase.test(GenericEmailTestCase.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 35 more

谁能告诉我我要去哪里错了。

Answers:


115

com.sun.mail.util.MailLoggerJavaMail API的一部分。它已经包含在EE环境中(这就是为什么您可以在实时服务器上使用它),但是它不包含在SE环境中。

Oracle文档

JavaMail API作为与Java SE平台一起使用的可选软件包提供,并且也包含在Java EE平台中。

99%的人在SE环境中运行测试,这意味着在运行测试时必须手动将其手动添加到类路径中。

如果您使用的是maven,请添加以下依赖项(您可能希望更改版本):

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.0</version>
</dependency>

63
javax.mail-api构件仅适用于编译。实际上,您需要运行代码,因此您需要JavaMail的完整实现。使用<groupId>com.sun.mail</groupId><artifactId>javax.mail</artifactId>
比尔·香农

149

javax.mail-api工件仅适用于编译

实际上,您需要运行代码,因此您需要JavaMail API 的完整实现。用这个:

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

注意:版本号可能会有所不同。在此处检查最新版本。


3
这也修复java.lang.NoClassDefFoundError: com/sun/mail/util/PropUtilCould not initialize class javax.mail.internet.InternetAddress错误。
GlenPeterson

1
JavaMail项目网页对此进行了说明。
Bill Shannon

52

我使用以下Maven依赖项来使Java邮件正常工作。第一个包括javax.mail API(无实现),第二个是javax.mail API的SUN实现。

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>javax.mail-api</artifactId>
    <version>1.5.5</version>
</dependency>
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.5.5</version>
</dependency>

9
如果您有实现,则无需包含api。
GlenPeterson

1
@GlenPeterson好点,我没有从上面的解决方案中意识到这一点,但是当我查看com.sun.mail jar时,其中包含了javax.mail api。
彼得(Peter




By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.