无法从 Windows XP 命令行运行 Java 程序 - ClassNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5334270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:27:19  来源:igfitidea点击:

Unable to run Java program from Windows XP command line - ClassNotFoundException

javawindowscommand-linewindows-xp

提问by remington123

I am trying to create and run a java program from Windows XP CMD line, which fails, by doing the following. Can anyone tell me what looks wrong / what else to try?

我正在尝试通过执行以下操作从 Windows XP CMD 行创建和运行 java 程序,但失败了。谁能告诉我什么看起来不对/还有什么可以尝试的?

C:\> java -version
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) Client VM (build 19.1-b02, mixed mode)

C:\> mkdir c:\j\
C:\> cd c:\j\
C:\j\> notepad Test.java

(opens Notepad where I insert the following text and Save, then Close.)

(打开记事本,在其中插入以下文本并保存,然后关闭。)

public class Test{
  public static void main(String[] args){
    System.out.println("hi");
  }
}


C:\j\> javac Test.java
C:\j\> java Test
Exception in thread "main" java.lang.NoClassDefFoundError: test
Caused by: java.lang.ClassNotFoundException: test
        at java.net.URLClassLoader.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: test.  Program will exit.

The real issue is that when I try to debug a project with Eclipse, I get an error that it "could not create the Java Virtual Machine", and I am trying to test compiling & running a program without the IDE first to be sure that my JDK installation works.

真正的问题是,当我尝试使用 Eclipse 调试项目时,我收到“无法创建 Java 虚拟机”的错误消息,我正在尝试先在没有 IDE 的情况下测试编译和运行程序,以确保我的JDK安装有效。



Update:You're correct that CLASSPATH is set on my machine, however I still get an Exception. Executing the suggested line produces:

更新:您在我的机器上设置了 CLASSPATH 是正确的,但是我仍然收到异常。执行建议的行会产生:

C:\j>java -cp . Test
Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: Test)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access
C:\j> ls
0(Unknown Source) at java.net.URLClassLoader.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: test. Program will exit.

回答by Ernest Friedman-Hill

It looks like the CLASSPATH environment variable has been set on your machine, and the value doesn't include the traditional "dot" (.) to represent the current directory. You can tell Java to look in the current directory like this:

看起来您的机器上已经设置了 CLASSPATH 环境变量,并且该值不包含表示当前目录的传统“点”(.)。您可以告诉 Java 在当前目录中查找,如下所示:

java -cp . Test

爪哇 -cp 。测试

(that's java space dash cp space dot space Test).

(那是java space dash cp space dot space Test)。

回答by Nanthini Muniapan

What does this command outputs on the screen?

这个命令在屏幕上输出什么?

##代码##

If you can't see Test.class, compile the program again i.e javac Test.javaand then execute it as java Test

如果看不到Test.class,重新编译程序ie javac Test.java,然后执行java Test

On edit:-

编辑时:-

It is advisable not to create classes with smaller cases. Please refer to Java coding convention. Try to manually remove Test.class. Recompile the code and run it again.

建议不要创建具有较小案例的类。请参考Java 编码约定。尝试手动删除 Test.class。重新编译代码并再次运行。

回答by Ernest Friedman-Hill

Answer to question 2, then: something you're showing us is not an actual cut-and-paste. The problem you're having is that the letter case used to name the "Test" class in the Java source and the letter case you're using to invoke the class on the command line don't match. Based on the error message, I'd say the source code says "class Test", but you're typing "java test" on the command line.

然后回答问题 2:您向我们展示的内容不是实际的剪切和粘贴。您遇到的问题是用于在 Java 源代码中命名“Test”类的字母大小写与用于在命令行上调用该类的字母大小写不匹配。根据错误消息,我会说源代码显示为“class Test”,但您在命令行上键入“java test”。

回答by remington123

This isn't really an answer because it isn't the desired behavior, but my class was defined (and being referenced in command-line commands as "Test" with a capital "T", and I was seeing a run-time error saying that class "test" with a lowercase "t" was not found. After a lot of trial and error, I renamed "Test.java" to "test.java" and edited the source file to define the class "test" instead of "Test". After that, I recompiled and ran the program with command-line command: "java test". Now it works. The installation of Java that I'm working with apparently doesn't allow classes whose names start with capital letters...?

这不是真正的答案,因为它不是所需的行为,但我的类已定义(并在命令行命令中被引用为带有大写“T”的“Test”,并且我看到了运行时错误说没有找到带有小写“t”的类“test”。经过多次反复试验,我将“Test.java”重命名为“test.java”并编辑源文件以定义类“test”的“测试”。之后,我重新编译并使用命令行命令运行程序:“java test”。现在它可以工作了。我正在使用的Java安装显然不允许名称以大写开头的类字母……?