java - 尝试从命令行运行时出现 NoClassDefFoundError

标签 java noclassdeffounderror

提前致歉 - 我知道有很多很多非常相似的问题,但我想问一些针对我的情况的问题。

我在同一目录中有一堆 java 和 jar 文件。我能够很好地编译,因此最终在同一个目录中得到了许多类文件。但是当我去执行程序时,它给出了一个 NoClassDefFoundError 说它找不到指定的类:

C:\Users\DB\Desktop\nextreports-integration-demo\src\ro\nextreports\integration>
java -cp ".;*.jar" SimpleDemo
Exception in thread "main" java.lang.NoClassDefFoundError: SimpleDemo (wrong nam
e: ro/nextreports/integration/SimpleDemo)

我在更高级别的目录中尝试了同样的事情,但没有任何区别:

C:\Users\DB\Desktop\nextreports-integration-demo\src>java -cp ".\ro\nextreports\
integration\*.jar;.\ro\nextreports\integration" SimpleDemo
Exception in thread "main" java.lang.NoClassDefFoundError: SimpleDemo (wrong nam
e: ro/nextreports/integration/SimpleDemo)

源文件中的打包语句为:

package ro.nextreports.integration;

我有一种感觉,我忽略了一些非常基本的东西。提前致谢。

编辑:非常感谢。它适用于以下内容:

java -cp ".\ro\nextreports\integration\nextreports-engine-6.3.jar;.\ro\nextreports\integration\commons-jexl-2.1.1.jar;.\ro\nextreports\integration\commons-logging-1.1.1.jar;.\ro\nextreports\integration\derby-10.10.1.1.jar;.\ro\nextreports\integration\itext-2.1.7.jar;.\ro\nextreports\integration\itext-rtf-2.1.7.jar;.\ro\nextreports\integration\itextpdf-5.0.6.jar;.\ro\nextreports\integration\jcalendar-1.3.2.jar;.\ro\nextreports\integration\jcommon-1.0.15.jar;.\ro\nextreports\integration\jfreechart-1.0.12.jar;.\ro\nextreports\integration\jofc2-1.0.1.jar;.\ro\nextreports\integration\mysql-connector-java-5.1.23-bin.jar;.\ro\nextreports\integration\mysql-connector-java-5.1.23-bin.jar;.\ro\nextreports\integration\poi-3.7.jar;.\ro\nextreports\integration\winstone-lite-0.9.10.jar;.\ro\nextreports\integration\xstream-1.3.1.jar;" ro.nextreports.integration.SimpleDemo

但为什么我不能对*.jar 文件使用通配符呢?例如,对于我未明确说明的任何 jar 文件中的类,以下会导致 NoClassDefFoundError:

java -cp ".;.\ro\nextreports\integration\*.jar" ro.nextreports.integration.
SimpleDemo

最佳答案

假设这是你的目录树:

src/
   ro/
     nextreports/
       integration/
         SimpleDemo.class

根据您的包声明,您编译的类应该在 integration 子目录中。检查该目录中是否确实存在 SimpleDemo.class。如果正确,则其类路径包含 src 目录的内容。

这意味着,如果您没有任何 JAR 依赖项,您可以像这样从 src 目录运行您的应用程序:

java -cp . ro.nextreports.integration.SimpleDemo

您需要使用完全限定的类名。

因为您确实有 jars,所以您也必须将它们包含在类路径中。假设您在 src 上面的目录中有 一个 JAR,在当前目录中有 另一个,您可以使用:

java -cp ../one.jar;another.jar;. ro.nextreports.integration.SimpleDemo

如果您从另一个目录运行它,如果您查看类路径的相对目录或使用绝对目录来描述您的类路径,它仍然可以工作。我不确定,但通常当前目录总是由 java 可执行文件包含在类路径中。

关于java - 尝试从命令行运行时出现 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22058621/

相关文章:

java - 从命令行执行 Java 程序时收到 "wrong name"NoClassDefFoundError

java - 包含外部项目的 NoClassDefFoundError - JBoss 7.1

java - 将组件放在表格列标题中?

java - 打印数组列表

java - 在java中产生太多线程

java - 为什么我在 Java 中收到 NoClassDefFoundError?

Hadoop 基础的 MapReduce 程序中的 java.lang.NoClassDefFoundError

java - 为什么我的 Java servlet 过滤器不能通过 HTTPS 工作?

java - 如何测试用户的输入以查看其单词是否在指定范围内

java - 添加 jai 作为 wildfly 模块