可执行 Jar 的 NoClassDefFoundError

发布于 2024-10-09 02:12:28 字数 1530 浏览 9 评论 0原文

可能是 exec jar 的一个非常常见的问题。我正在尝试创建并运行一个可执行的 jar,它让我发疯。

我有几个类(带有 stmts 包),其中一个有一个 main 方法,一个简单的空构造函数,当然还有一些 biz 方法,这些构成了一个小型 eclipse 项目的一部分。
我使用 build.xml 将这些文件和清单文件捆绑到 jar(arc.jar) 中。我的程序使用 jdom 库以及来自我的其他 eclipse 项目之一的引用,因此我在构建 arc.jar 时同时包含 jdom 库和其他 biz 库。

这是我写的Manifest.MF,Main-Class后面有一个新行当

Manifest-Version: 1.2  
Class-Path: jdom.jar other.jar
Main-class: uk.co.Art 

我解压这个arc.jar时,它包含jdom.jar,other.jar,META-INF/Manifest.mf和我的类的包。 有趣的是,jar 中的 Manifest 文件看起来像这样 -

Manifest-Version: 1.2 
Created-By: 14.2-b01 (Sun Microsystems Inc.) 
Main-class: uk.co.Art
Class-Path: jdom.jar other.jar

当 Main 类的值没有包时,我得到了 ClassNotFoundException ClassNotFoundException: uk.co.Art 。使用包时,错误更改为 ClassNotFoundException: org.jdom.JDOMException
因此,出于测试目的,我尝试为我的执行类和 jar 提供 c:\ 的完整路径,但仍然无法找到主类,出现错误 ClassNotFoundException: uk .co.Art.
我似乎无法理解为什么。
我使用的命令行是 - java -jar Arc.jar
cmd 提示符上的版本是 -
java 版本“1.6.0_16”
Java(TM) SE 运行时环境(内部版本 1.6.0_16-b01)
Java HotSpot(TM) 客户端 VM(版本 14.2-b01,混合模式)

而 eclipse 安装的 jre 是 jre6

你们有什么建议?

Alex 的解决方案确实解决了本地执行的问题。
我还在考虑另一种执行方式(作为额外选项),在服务器上部署 Arc.jar 并在该机器上运行批处理,该批处理基本上设置 java 类路径,然后以任何方式调用 jar 或直接调用类作品。 但这只是一种选择,而不是首选。
或者,也可以在我的清单中设置该类路径并在本地构建 jar 并部署在服务器上。
不过还是想尝试一些事情。 但现在我明白,基本问题是 jar 到 jar 中,因此是类路径问题。

非常感谢大家。

Probably a very usual problem with exec jar. I am trying to create and run an executable jar and its driving me crazy.

I have couple of classes(with package stmts), one of which has a main method, a simple empty constructor and ofcourse few biz methods, these form part of a small eclipse project.
I am bundling both these and a manifest file into a jar(arc.jar) using a build.xml. My program uses jdom library and also references from one of my other eclipse projects, so I am including both the jdom libray and other biz library while building my arc.jar.

This is the Manifest.MF I wrote, there is a new lineafter Main-Class

Manifest-Version: 1.2  
Class-Path: jdom.jar other.jar
Main-class: uk.co.Art 

When I unzip this arc.jar, it contains jdom.jar, other.jar, META-INF/Manifest.mf and the package with my classes.
Funnily, the Manifest file in jar looks like this -

Manifest-Version: 1.2 
Created-By: 14.2-b01 (Sun Microsystems Inc.) 
Main-class: uk.co.Art
Class-Path: jdom.jar other.jar

I got the ClassNotFoundException ClassNotFoundException: uk.co.Art when the value of Main class was without the package. With package the error changed to ClassNotFoundException: org.jdom.JDOMException.
So just for testing purpose, I tried giving the fullpaths with c:\ for both my executing class and the jars but still it is not able to locate the main class, errors out ClassNotFoundException: uk.co.Art.
I cant seem to understand why.
The command line I am using is - java -jar Arc.jar
The version on cmd prompt is -
java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode)

while the eclipse installed jre is jre6

What can you folks suggest ?

Alex' solution did solve the problem for local execution.
I am also thinking of another way(as an extra option) of execution, to deploy the Arc.jar on server and run a batch on that machine which basically sets the java classpath and then calls either the jar or directly the class whichever way it works. But its only an option, not a preferred one though.
Or, also, set that classpath in my manifest and build jar locally and deploy on the server.
Would like to try out a few things though. But now I understand that the basic problem was jars into jar and hence classpath issue.

Thanks a lot folks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那小子欠揍 2024-10-16 02:12:28

您不能将其他 JAR 捆绑到一个 JAR 中。它们必须位于同一文件夹中,或者至少位于与 Arc.jar 相关的文件夹中。然后,您可以在Class-Path 条目中指定其相对路径。

然而,Eclipse 有一个有用的导出工具,它允许您将其他 JAR 的内容嵌入到您的 JAR 中,这样您就不会得到多个需要放置在特定位置的松散 JAR。当您选择导出>时,选中第二个库处理选项。可运行的 JAR 文件。

alt text

当选择将所需的库打包到生成的 JAR 时,Eclipse 将添加一个特殊的类加载器,它将在执行主类之前透明地为您加载这些 JAR。它是在 JarRsrcLoader

You cannot bundle other JAR's in a JAR. They have to go in the same folder or at least in a folder relative to the Arc.jar. You specify then its relative path in the Class-Path entry.

Eclipse however has an useful export tool which allows you to embed the contents of other JAR's in your JAR so that you don't end up with multiple loose JAR's which are required to be placed in a specific location. Check the 2nd Library Handling option when you choose Export > Runnable JAR file.

alt text

When choosing Package required libraries into generated JAR, then Eclipse will add a special classloader which will load those JAR's transparently for you before executing the main class. It's doing that with help of JarRsrcLoader.

小耗子 2024-10-16 02:12:28

您不必将额外的罐子装入您的罐子中。这些 jar 应该与您的应用程序 jar 位于同一目录中。换句话说:将所有 jar 放入一个目录并运行。一切都会成功。

原因:想想可执行jar是什么意思。如果您的 jar 不是“可执行的”,请按如下方式运行它:

java -cp art.jar;ldom.jar;other.jar uk.co.Art

当您的 jar 可执行时,您使用以下命令行:

java -jar art.jar

没有魔法:jvm 打开清单并从那里获取主类和其他 jar 的名称,并像第一个一样创建命令行一。现在您了解了您的案例中的所有 jar 都必须位于同一目录中。

You do not have to pack your additional jars into your jar. These jars should be in the same directory as your application jar. By other words: put all jars into one directory and run. Everything will work.

Reasons: think what does executable jar mean. If you jar is not "executable" you run it as following:

java -cp art.jar;ldom.jar;other.jar uk.co.Art

when your jar is executable you use the following command line:

java -jar art.jar

No magic: jvm opens manifest and takes name of main class and other jars from there and virtually creates command line like first one. Now you understand that all jars in your case must be in the same directory.

吹梦到西洲 2024-10-16 02:12:28

也许 .jar 已损坏,就像我有时发生的那样。看看这个

Maybe the .jar is corrupt, like sometimes happened to me. Take a look at this.

a√萤火虫的光℡ 2024-10-16 02:12:28

导出时需要注意以下几点。

  1. 与项目关联的 JRE 与用于运行该项目的 JRE 一致。

  2. 请将您的项目导出到本地驱动器,然后将其移动到任何共享驱动器(这解决了我的问题)。

谢谢。

Just couple of things to be noted when exporting.

  1. JRE associated to the project is consistent with the JRE that would be used to run it.

  2. Please export your project into a local drive, then move it into any shared drive (this fixed my issue).

Thank you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文