gpt4 book ai didi

java - 尝试使用 java.exe -jar 运行我的 jar 时出现 NoClassDefFoundError ...怎么了?

转载 作者:IT老高 更新时间:2023-10-28 20:25:01 24 4
gpt4 key购买 nike

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

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 文件(是的,确实存在):

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 文件的内容:

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 list 的内容:

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 或打包 jar 的工具),第三方解决方案可能已经出现。最终目标是创建一个 jar,它可以从我的开发 Windows 机器复制到 Linux 服务器(带有任何依赖的 jar)并用于填充数据库(因此类路径可能最终在开发和部署环境之间有所不同)。任何有关此谜团的线索将不胜感激!

最佳答案

-jar 选项与-classpath 互斥。查看旧描述 here

-jar

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.

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.

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

一个快速而肮脏的技巧是将你的类路径附加到引导类路径:

-Xbootclasspath/a:path

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

但是,如 @Dan正确地说,正确的解决方案是确保您的 JAR list 包含它需要的所有 JAR 的类路径。

关于java - 尝试使用 java.exe -jar 运行我的 jar 时出现 NoClassDefFoundError ...怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/250166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com