使用 JAR 的 Java 错误,java.lang.NoClassDefFoundError: javax/mail/Store

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

Java error using JARs, java.lang.NoClassDefFoundError: javax/mail/Store

javajarexecutable-jar

提问by Rob

I'm making a java application that uses Javamail, and it works great when I compile it. I want to make it into a jar file so it can be easily moved around and executed. The problem is I am getting this error when I try to run the jar from the cmd line

我正在制作一个使用 Javamail 的 Java 应用程序,当我编译它时效果很好。我想把它做成一个 jar 文件,这样它就可以很容易地移动和执行。问题是当我尝试从 cmd 行运行 jar 时出现此错误

java -jar ActriveTray2.jar

java -jar ActiveTray2.jar

Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Store
        at stockApp.init(stockApp.java:11)
        at ActiveTray.main(ActiveTray.java:31)
        Caused by: java.lang.ClassNotFoundException: javax.mail.Store
        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)

Here's what's in my jar file

这是我的 jar 文件中的内容

  • images/tray.gif
  • META-INF/MANIFEST.MF
  • ActiveTray.class
  • ActiveTray.java
  • config.class
  • config.java
  • GmailFetch.class
  • GmailFetch.java
  • stockApp$1.class
  • stockApp.class
  • stockApp.java
  • 图像/托盘.gif
  • 元信息/清单.MF
  • 活动托盘类
  • 活动托盘.java
  • 配置类
  • 配置文件
  • GmailFetch.class
  • GmailFetch.java
  • stockApp$1.class
  • stockApp.class
  • 股票应用程序

I think the issue is classpath, where the jar doesn't know where the javamail jar lives. My classpath is set correctly when I execute the code (java ActiveTray2) it works fine... help :(

我认为问题在于类路径,jar 不知道 javamail jar 所在的位置。当我执行代码(java ActiveTray2)时,我的类路径设置正确...帮助:(

回答by Paul Sanwald

you need to add the jars you depend on to the manifest file, and also add them to the jar you are creating. java -jar will ignore your environment's classpath, by design. From the java docs:

您需要将您依赖的 jar 添加到清单文件中,并将它们添加到您正在创建的 jar 中。java -jar 将按照设计忽略您环境的类路径。 从java文档:

-jar: When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

-jar:使用该选项时,JAR 文件是所有用户类的来源,其他用户类路径设置将被忽略。

so, you can add a line to your manifest file like:

因此,您可以在清单文件中添加一行,例如:

Class-Path: mail.jar

to your manifest file.

到您的清单文件。

回答by Snicolas

When you launch a runnable jar file in java, the classpath is ignored, both the environment variable and the command line classpath. You have to specify it inside the manifest file of your runnable jar file.

在 java 中启动可运行的 jar 文件时,将忽略类路径,包括环境变量和命令行类路径。您必须在可运行 jar 文件的清单文件中指定它。

Here is some information from wikipedia:

以下是维基百科的一些信息:

You manifest has to include :

您的清单必须包括:

Main-Class: org.mypackage.HelloWorld
Class-Path: lib/supportLib.jar
<additional line feed and carriage return>

The class-path will list the location of the librairies you use relative to the location of your main runnable jar file.

类路径将列出您使用的库相对于主要可运行 jar 文件的位置的位置

Regards, Stéphane

问候, 斯蒂芬

回答by rahulmohan

The required mail implementation jars should be present in your classpath.

所需的邮件实现 jars 应该存在于您的类路径中。

回答by RED.Skull

Same problem i faced in my project. The reason is jar file missing or not locate in class path. Two steps to solve this issue.

我在我的项目中遇到了同样的问题。原因是 jar 文件丢失或不在类路径中。解决这个问题的两个步骤。

  1. First add JavaMail API 1.4.7 jarto the class path.

  2. Then add the same jar to server library. In my application i used Apache tomcat 7. i put the jar in libfolder

  1. 首先将JavaMail API 1.4.7 jar添加到类路径中。

  2. 然后将相同的 jar 添加到服务器库。在我的应用程序中,我使用了 Apache tomcat 7。我将 jar 放在lib文件夹中