简体   繁体   English

尝试从命令行运行时NoClassDefFoundError

[英]NoClassDefFoundError when attempting to run from command line

Apologies in advance - I know there are many, many, many very similar questions but I wanted to ask something specific to my situation. 提前道歉 - 我知道有很多很多非常相似的问题,但我想问一些与我的情况有关的问题。

I have a bunch of java and jar files in the same directory. 我在同一目录中有一堆java和jar文件。 I am able to compile fine and thus end up with a number of class files in the same dir. 我能够编译好,因此最终在同一个目录中有许多类文件。 But when I go to execute the program it gives a NoClassDefFoundError saying it can't find the specified class: 但是当我去执行程序时,它会给出一个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)

I tried the same thing from a higher-level dir but it made no difference: 我从更高级别的目录尝试了同样的事情,但它没有任何区别:

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)

The package statement in the source file is: 源文件中的package语句是:

package ro.nextreports.integration;

I have a feeling I'm overlooking something very elementary. 我有一种感觉,我忽略了一些非常基本的东西。 Thanks in advance. 提前致谢。

Edit: Thanks very much. 编辑:非常感谢。 It works with the following: 它适用于以下内容:

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

But why can I not use wildcards for the *.jar files? 但为什么我不能在* .jar文件中使用通配符? For instance, the following leads to a NoClassDefFoundError for a class in any jar file I don't make explicit: 例如,以下导致任何jar文件中的类的NoClassDefFoundError我没有明确说明:

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

Suppose this is your directory tree: 假设这是你的目录树:

src/
   ro/
     nextreports/
       integration/
         SimpleDemo.class

From your package declaration, your compiled class should be in the integration subdiretory. 从您的包声明中,您编译的类应该在integration子目录中。 Check that there really is a SimpleDemo.class in that directory. 检查该目录中是否确实存在SimpleDemo.class If that is correct, its classpath includes the contents of the src directory. 如果这是正确的,它的类路径包括src目录的内容。

That means that, if you didn't have any JAR dependencies, you could run your application from the src directory like this: 这意味着,如果您没有任何JAR依赖项,您可以从src目录运行您的应用程序,如下所示:

java -cp . ro.nextreports.integration.SimpleDemo

You need to use the fully qualified class name. 您需要使用完全限定的类名。

Since you do have jars, you have to include them in the classpath as well. 既然你有jar,你也必须将它们包含在类路径中。 Suppose you have one JAR in the directory above src, and another in the current directory, you could use: 假设你有one以上的src目录JAR,和another在当前目录下,你可以使用:

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

If you run it from another directory, it will still work if you review the classpath's relative directories or use absolute directories to describe your classpath. 如果从另一个目录运行它,如果查看类路径的相关目录或使用绝对目录来描述类路径,它仍然可以工作。 I am not sure but usually the current directory is always included in the classpath by the java executable. 我不确定,但通常当前目录始终由java可执行文件包含在类路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在命令行上运行 Java Spark 应用程序时出现 NoClassDefFoundError - NoClassDefFoundError when attempting to run Java Spark app on command line 当我从 Linux 的命令行运行 Java class 文件时,为什么会出现 NoClassDefFoundError? - Why am I getting a NoClassDefFoundError when I run a Java class file from the command line in Linux? Maven从命令行运行批处理,java.lang.NoClassDefFoundError - Maven run batch from command line, java.lang.NoClassDefFoundError 尝试在Eclipse中运行时,LWJGL中的java.lang.NoClassDefFoundError - java.lang.NoClassDefFoundError in LWJGL when attempting to run in Eclipse 尝试从jar从命令行运行Usergrid获取NoClassDefFoundError - Try to run Usergrid from command-line from jar get NoClassDefFoundError 从带有包的命令行运行Java:NoClassDefFoundError - Running java from command line with packages: NoClassDefFoundError 从命令行运行时,堆栈跟踪中没有换行符吗? - No newlines in stacktrace when run from command line? 命令行中的Java NoClassDefFoundError - Java NoClassDefFoundError in Command Line Java命令行中的NoClassDefFoundError - NoClassDefFoundError in java command line 从命令行执行Java程序时收到“名称错误” NoClassDefFoundError - Receiving “wrong name” NoClassDefFoundError when executing a Java program from the command-line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM