简体   繁体   中英

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

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?

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$1.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.


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

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$000(Unknown Source)
        at java.net.URLClassLoader$1.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.

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:

java -cp . Test

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

What does this command outputs on the screen?

C:\j> ls

If you can't see Test.class, compile the program again ie javac Test.java and then execute it as 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM