java 获取 javax.mail.MessagingException

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

getting javax.mail.MessagingException

javaemailexception

提问by ramesh

I am using javax.mail .jar file to read the mail messages. But when i m running the code i am getting the following exception.

我正在使用 javax.mail .jar 文件来读取邮件消息。但是当我运行代码时,我收到以下异常。

I added mail.jar in classpath.

我在类路径中添加了 mail.jar。

Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/MessagingE
xception
Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
        at java.net.URLClassLoader.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: MasterProcess.  Program will exit.

any suggetionsn to solve this issue please ...

任何解决此问题的建议,请...

回答by Joeri Hendrickx

You're not getting a MessagingException, the VM is complaining that it can't findMessagingException(although it's probably that it's looking for it because it wants to throw it, but those are issues for later).

你没有得到MessagingException,VM 抱怨它找不到MessagingException(虽然它可能正在寻找它,因为它想抛出它,但这些都是以后的问题)。

Check if your mail.jar actually contains this class, and check if your mail.jar really is on the classpath.

检查您的mail.jar 是否真的包含这个类,并检查您的mail.jar 是否真的在类路径中。

The last thing that could happen is that the class is incompatible with your version of Java. Classes compiled for 1.5 won't run on 1.4, for example.

可能发生的最后一件事是该类与您的 Java 版本不兼容。例如,为 1.5 编译的类不会在 1.4 上运行。

回答by Karthikeyan Rajendran

Working in eclipse and not in command line clearly say that there is multiple versions of mail jar present in the project.

在 eclipse 中而不是在命令行中工作清楚地表明项目中存在多个版本的邮件 jar。

Ex:

前任:

You have 3 Jars , Jar X, Jar Y and Jar Z.

你有 3 个罐子,罐子 X,罐子 Y 和罐子 Z。

With out you knowing JAR X might be already be bundled inside JAR Z ( in this case mail jar but some different version.

如果您不知道 JAR X 可能已经捆绑在 JAR Z 中(在这种情况下是邮件 jar,但有一些不同的版本。

So what is the problem in having multiple version of same jar ?You have two different versions and you will not know which will be referenced in your project. (so if you are looking for some class from mail-2.jar in your project you will get class not found exception if the reference is made to mail-1.jar by class loader)

那么有多个版本的同一个 jar 有什么问题呢?您有两个不同的版本,您不知道将在您的项目中引用哪个版本。(因此,如果您正在项目中从 mail-2.jar 中查找某个类,如果类加载器对 mail-1.jar 进行了引用,则会出现 class not found 异常)

So how come its working properly on Eclipse ?In eclipse you can see there is an option for ORDER the library, these will be reference in the same order, but while running in command prompt we used to load the as lib/* which loads all to gather and we will not know which lib will get loaded first.

那么它如何在 Eclipse 上正常工作呢?在 Eclipse 中,您可以看到有一个 ORDER 库的选项,这些将按相同的顺序引用,但是在命令提示符下运行时,我们曾经加载 as lib/* 加载所有要收集的库,我们不知道哪个库将首先加载。

How to identify the culprit and fix the issue ?

如何找出罪魁祸首并解决问题?

Option 1 :

选项1 :

See docs page / user guide of the libs you are using to see what they have in them.

请参阅您正在使用的库的文档页面/用户指南,以查看其中包含的内容。

Option 2 :

选项2:

  1. In eclipse move the JAR in question (mail.jar here) to TOP in the class path order. (your program should work now)
  2. Now bring down the order one by one to see where you get the error. (if you wish to find the jar which is causing this issue , duplicate reference)

  3. Extract the jar which has the duplicate reference remove the duplicate inside the JAR and repackage it. (if required)

  1. 在 Eclipse 中,将有问题的 JAR(此处为 mail.jar)按类路径顺序移动到 TOP。(你的程序现在应该可以工作了)
  2. 现在把顺序一一降低,看看哪里出错了。(如果您想找到导致此问题的 jar,请重复参考)

  3. 提取具有重复引用的 jar 删除 JAR 中的重复项并重新打包。(如果需要的话)

回答by BigMike

Also be sure you're having also the java activation.jarjar.

还要确保您还有 java activation.jarjar。

Are you by any chance working with an App Server (eg. Jboss), if so, just check classloading model (in jboss with single classloading model you may be loading a not so up-to-date version of the jars)

您是否有机会使用应用程序服务器(例如 Jboss),如果是这样,只需检查类加载模型(在具有单个类加载模型的 jboss 中,您可能正在加载不是最新版本的 jars)

回答by tsenapathy

I've also faced the same error. It happens to me that while compiling i used the jars correctly. While running it on command line i noticed the mail.jar (javamail API) was not included.

我也遇到过同样的错误。我碰巧在编译时正确使用了罐子。在命令行上运行它时,我注意到不包含 mail.jar(javamail API)。