java.lang.NoClassDefFoundError: javax/mail/MessagingException 未解决

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

java.lang.NoClassDefFoundError: javax/mail/MessagingException unsolved

javamavenjavax.mail

提问by Rafael Reyes

I'm trying to add the javax.mail jar to my classpath but I get this error:

我正在尝试将 javax.mail jar 添加到我的类路径,但出现此错误:

java.lang.NoClassDefFoundError: javax/mail/MessagingException
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2663)
    at java.lang.Class.getDeclaredConstructors(Class.java:2012)
    at org.springframework.beans.factory.annotation.

I've readed in other post that it could be fixed adding the dependecy to the pom.xml, so I did it:

我在其他帖子中读到可以修复将依赖项添加到 pom.xml,所以我做到了:

pom.xml

pom.xml

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.3</version>
</dependency>

project

项目

But still I'm getting the error ..can someone help me with this?

但我仍然收到错误..有人可以帮助我吗?

采纳答案by vanval

Also with the 1.4.3 version there is no longer an artifact id called mail. If you want to use 1.4.3 you should use this dependency

同样在 1.4.3 版本中,不再有名为 mail 的工件 ID。如果你想使用 1.4.3 你应该使用这个依赖

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mailapi</artifactId>
    <version>1.4.3</version>
</dependency>

回答by Harald Wellmann

The most recent version of the Java Mail API is

Java Mail API 的最新版本是

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>javax.mail-api</artifactId>
    <version>1.5.2</version>
</dependency>

Do you get the same error with this version?

你这个版本有同样的错误吗?

回答by fateddy

the javax/mail/MessagingExceptionalready exists in version 1.4. So I guess this is just an assembly issue within the IDE. Refreshing the pom dependencies and checking the artifact the IDE builds up should be sufficient.

javax/mail/MessagingException已经存在于1.4版本。所以我想这只是 IDE 中的一个程序集问题。刷新 pom 依赖项并检查 IDE 构建的工件应该就足够了。

回答by user207421

This class has been present in all versions of Javamail I have ever used. Clearly you don't have the Javamail JAR(s) on your CLASSPATH, as for any other ClassNotFoundException.

这个类已经出现在我使用过的所有 Javamail 版本中。很明显,您的 CLASSPATH 上没有 Javamail JAR,就像任何其他 ClassNotFoundException 一样。

回答by Jens

You mixed the classpath by using maven and manually added references in eclipse.

您使用 maven 混合了类路径,并在 eclipse 中手动添加了引用。

Disable the "maven nature" make a mvn eclipse:cleanon commandline refresh your project and convert it to maven projectand then try again.

禁用“Maven 自然”使mvn eclipse:clean命令行刷新您的项目convert it to maven project,然后重试。

To clearify: Where have you added the mail api in pom? under <dependecies>or under <dependecyManagement?

要清除:您在 pom 中在哪里添加了邮件 api?下<dependecies>还是下<dependecyManagement

回答by ayad laouissi jones

You should add the jar to the server Tomcat.

您应该将 jar 添加到服务器 Tomcat。

  1. Open launch configuration

    You should open launch configuration

  2. Add external jar (look for your email jar)

    Add external jar and look for your email jar

  1. 打开启动配置

    您应该打开启动配置

  2. 添加外部 jar(查找您的电子邮件 jar)

    添加外部 jar 并查找您的电子邮件 jar

回答by Manan Shah

Even I was facing a similar error. Try below 2 steps (the first of which has been recommended here already) - 1. Add the dependency to your pom.xml

甚至我也面临着类似的错误。尝试以下 2 个步骤(此处已推荐第一个步骤)- 1. 将依赖项添加到您的pom.xml

         <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.3</version>
        </dependency>

  1. If that doesn't work, manually place the jarfile in your .m2\repository\javax\<folder>\<version>\directory.
  1. 如果这不起作用,请手动将该jar文件放在您的.m2\repository\javax\<folder>\<version>\目录中。

回答by Gabriele D'Agostino

You will use maven-dependency-pluginto copy project dependencies to somewhere else.

您将使用maven-dependency-plugin将项目依赖项复制到其他地方。

Add this to pom.xml

将此添加到 pom.xml

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
      <archive>
        <manifest>
        <addClasspath>true</addClasspath>
        <mainClass>com.mkyong.core.App</mainClass>
        <classpathPrefix>dependency-jars/</classpathPrefix>
        </manifest>
      </archive>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
      <execution>
        <id>copy-dependencies</id>
        <phase>package</phase>
        <goals>
            <goal>copy-dependencies</goal>
        </goals>
        <configuration>
            <outputDirectory>
                          ${project.build.directory}/dependency-jars/
                    </outputDirectory>
        </configuration>
        </execution>
    </executions>
  </plugin>

Following manifest file will be generated. The project dependencies will be copied to {project}/target/dependency-jars/.

将生成以下清单文件。项目依赖将被复制到 {project}/target/dependency-jars/。

manifest-Version: 1.0
Built-By: mkyong
Build-Jdk: 1.6.0_35
Class-Path: dependency-jars/log4j-1.2.17.jar
Created-By: Apache Maven
Main-Class: com.mkyong.core.App
Archiver-Version: Plexus Archiver

This worked for me. Source: MKyong.com

这对我有用。资料来源:MKyong.com