开发者

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百科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).


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.


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...?


What does this command outputs on the screen?

C:\j> ls

If you can't see Test.class, compile the program again i.e 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜