带有可执行jar的NoClassDefFoundError [英] NoClassDefFoundError with Executable Jar

查看:226
本文介绍了带有可执行jar的NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

exec 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.

我有几个类(包stmts),其中一个有一个main方法,一个简单的空构造函数和几个biz方法,这些构成了一个小eclipse项目的一部分。

我使用build.xml将这些和一个清单文件捆绑到一个jar(arc.jar)中。我的程序使用jdom库以及我的其他一个eclipse项目的引用,所以我在构建我的arc.jar时包括jdom libray和其他商业库。

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.

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

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 

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

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

当Main类的值没有时,我得到了ClassNotFoundException ClassNotFoundException:uk.co.Art 包。使用包时,错误更改为 ClassNotFoundException:org.jdom.JDOMException

因此,出于测试目的,我尝试使用<$ c $给出完整路径c> c:\ 对于我的执行类和jar,但仍然无法找到主类,错误输出 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

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

你们有什么建议?


Alex'解决方案确实解决了本地执行的问题。

我也在考虑另一种方式(作为额外选项)执行,在服务器上部署Arc.jar并在该机器上运行批处理ine基本上设置java类路径然后调用jar或直接调用它的工作方式。 但它只是一个选项,不是首选的选项。

或者,也可以在我的清单中设置该类路径并在本地构建jar并部署在服务器。

想尝试一些东西。 但现在我明白基本的问题是罐子里的罐子,因此是类路径问题。

非常感谢大家。

推荐答案

您无需将额外的罐子装入罐子里。这些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.

理由:想想可执行jar是什么意思。如果jar不是可执行,则按以下方式运行:

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

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

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

java -jar art.jar

没有魔法:jvm打开清单并从那里获取主类和其他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.

这篇关于带有可执行jar的NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆