本文发表在 rolia.net/zh 相约加拿大网上社区枫下论坛
Q: I am trying to learn Java. I copied a Hello.java example to C:\sources and then succeeded in compiling the course file into the class file. Then, I tried to run this example by entering "java Hello". However, I got an effor message saying "....java.lang.NoClassDefFoundError:...". What's wrong?
A: Very probably, your Windows envirenment variable CLASSPATH does not include ".", --- the current directory. To execute your command "java Hello", Java Virtual Machine needs to search the class file Hello.class according to CLASSPATH. If it fails to find the class file, it will complain by throwing the message you cited. For a Java programmer, it is a good practice to include the current directory in the CLASSPATH.
To make your example work, you can reset the CLASSPATH by issuing
in the MS-DOS. The above command means to append . (the current directory) to the CLASSPATH.
Note: Windowes envirment variable should be sandwished by a pair of percentage signs (%). After you reset the CLASSPATH, you will have luck to run your example.
PATH and CLASSPATH are two most basic environment variables for Java programmers. Even seasoned Java developers can get into dark with them. A Java novice needs to make many mistakes with them before mastering them.
更多精彩文章及讨论，请光临枫下论坛. 网址: rolia.net/zh
This post has been archived. It cannot be replied.