尝试使用java.exe -jar运行我的jar时出现NoClassDefFoundError ...出了什么问题? [英] NoClassDefFoundError while trying to run my jar with java.exe -jar...what's wrong?

查看:306
本文介绍了尝试使用java.exe -jar运行我的jar时出现NoClassDefFoundError ...出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我正在尝试将其包装到jar中以便于部署。当作为一组可从CLASSPATH访问的类运行时,应用程序编译并运行正常(在Windows cmd窗口中)。但是当我将我的类加起来并尝试在同一个cmd窗口中使用java 1.6运行它时,我开始获得异常:

I have an application that I'm trying to wrap into a jar for easier deployment. The application compiles and runs fine (in a Windows cmd window) when run as a set of classes reachable from the CLASSPATH. But when I jar up my classes and try to run it with java 1.6 in the same cmd window, I start getting exceptions:

C:\dev\myapp\src\common\datagen>C:/apps/jdk1.6.0_07/bin/java.exe -classpath C:\myapp\libs\commons -logging-1.1.jar -server -jar DataGen.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at com.example.myapp.fomc.common.datagen.DataGenerationTest.<clinit>(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    ... 1 more

有趣的是,违规的LogFactory似乎在commons-logging-1.1.jar中,它位于指定的类路径中。 jar文件(是的,它确实存在):

The funny thing is, the offending LogFactory seems to be in commons-logging-1.1.jar, which is in the class path specified. The jar file (yep, it's really there):

C:\dev\myapp\src\common\datagen>dir C:\myapp\libs\commons-logging-1.1.jar
 Volume in drive C is Local Disk
 Volume Serial Number is ECCD-A6A7

 Directory of C:\myapp\libs

12/11/2007  11:46 AM            52,915 commons-logging-1.1.jar
           1 File(s)         52,915 bytes
           0 Dir(s)  10,956,947,456 bytes free

commons-logging-1.1.jar文件的内容:

The contents of the commons-logging-1.1.jar file:

C:\dev\myapp\src\common\datagen>jar -tf C:\myapp\libs\commons-logging-1.1.jar
META-INF/
META-INF/MANIFEST.MF
org/
org/apache/
org/apache/commons/
org/apache/commons/logging/
org/apache/commons/logging/impl/
META-INF/LICENSE.txt
META-INF/NOTICE.txt
org/apache/commons/logging/Log.class
org/apache/commons/logging/LogConfigurationException.class
org/apache/commons/logging/LogFactory$1.class
org/apache/commons/logging/LogFactory$2.class
org/apache/commons/logging/LogFactory$3.class
org/apache/commons/logging/LogFactory$4.class
org/apache/commons/logging/LogFactory$5.class
org/apache/commons/logging/LogFactory.class
... (more classes in commons-logging-1.1 ...)

是的,commons-logging具有LogFactory类。最后,我的jar清单的内容:

Yep, commons-logging has the LogFactory class. And finally, the contents of my jar's manifest:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 10.0-b23 (Sun Microsystems Inc.)
Main-Class: com.example.myapp.fomc.common.datagen.DataGenerationTest
Class-Path: commons-logging-1.1.jar commons-lang.jar antlr.jar toplink
 .jar GroboTestingJUnit-1.2.1-core.jar junit.jar

这让我感到难过,而且我现在已经超过一天的任何同事了。只是为了剔除答案,至少现在,由于许可限制和公司政策(例如:创建exe或包装罐子的工具),第三方解决方案可能已经解决了。最终目标是创建一个可以从我的开发Windows框复制到Linux服务器(带有任何相关的jar)并用于填充数据库的jar(因此类路径可能会在开发和部署环境之间变得不同)。任何有关这个谜团的线索都将不胜感激!

This has stumped me, and any coworkers I've bugged for more than a day now. Just to cull the answers, for now at least, third party solutions to this are probably out due to licensing restrictions and company policies (e.g.: tools for creating exe's or packaging up jars). The ultimate goal is to create a jar that can be copied from my development Windows box to a Linux server (with any dependent jars) and used to populate a database (so classpaths may wind up being different between development and deployment environments). Any clues to this mystery would be greatly appreciated!

推荐答案

-jar选项与-classpath互斥。请参阅旧说明此处

The -jar option is mutually exclusive of -classpath. See an old description here


-jar

-jar

执行封装在JAR文件中的程序。第一个参数是JAR文件的名称,而不是启动类名称。为了使此选项有效,JAR文件的清单必须包含Main-Class:classname形式的一行。这里,classname标识具有public static void main(String [] args)方法的类,该方法充当应用程序的起点。

Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point.

请参阅Jar工具参考页面和有关使用Jar文件和Jar文件清单的信息,请参阅Java Tutorial的Jar跟踪。

See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests.

使用此选项时,JAR文件为所有用户类的来源和其他用户类路径设置都将被忽略。

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

快速和脏黑客是将你的类路径追加到bootstrap类路径:

A quick and dirty hack is to append your classpath to the bootstrap classpath:


-Xbootclasspath / a: path

-Xbootclasspath/a:path

指定以冒号分隔的directires,JAR存档和ZIP存档路径,以附加到默认的引导程序类路径。

Specify a colon-separated path of directires, JAR archives, and ZIP archives to append to the default bootstrap class path.

然而,正如 @Dan 正确地说,正确的解决方案是确保您的JARs M. anifest包含它需要的所有JAR的类路径。

However, as @Dan rightly says, the correct solution is to ensure your JARs Manifest contains the classpath for all JARs it will need.

这篇关于尝试使用java.exe -jar运行我的jar时出现NoClassDefFoundError ...出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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