Some beginners appear when running programs with the Java HelloWorld directive:
Exception in thread "main" Java.lang.NoClassDefFoundError:HelloWorld
Or, when compiling the Javac Helloworld.java program, you will see the following error message:
Javac: Invalid token: HelloWorld. class <options> <source files>-help for listing
This error occurs primarily due to an environment variable configuration error.
(a) First, the compilation is the error prompt, meaning that the Helloworld.class file can not be found, at this point need to pay attention to check:
1. The path to the compile time is located in the same directory as the Helloworld.java program path. This error occurs if the current working directory is C: \ and the program is placed in the C:\Test directory.
2. Check that the Helloworld.java file name specified in the directive is consistent with the class name.
3. If you use the instruction in the C: \ Directory, Javac Helloworld.java compile the C:\Test\HelloWorld.java code, you need to specify the full path of the Helloworld.java. namely: Javac C:\Test\HelloWorld.java.
(b) for running Java HelloWorld Error, the main is not to configure the current directory into the CLASSPATH environment variables (the specific configuration method please Baidu).
If you imagine compiling, Java C:\Test\HelloWorld, which is the command, specifies the Helloworld.java full path, even if the current directory is C:\Test\. Obviously C:\Test\HelloWorld is a. class file (assuming the Helloworld.class file has been compiled correctly), why can't I find it? It turns out that Java treats. java files differently from. class files. The. java file can specify the path directly to it, and the. class file required by the Java command cannot appear with the extension, nor can you specify an additional path to it.
So how do you specify the path? The. class file that is required for Java must be specified by Classpath. Add a dot to the CLASSPATH environment variable (that is:.) This means that the current path is configured in Classpath.
(iii) Interpretation of environment variables related to Java
1. Path environment variable. The function is to specify the command search path, execute the command below the command line, such as Javac compiling the Java program, it will look in the path specified by the paths variable to see if the corresponding command program can be found. We need to add the bin directory in the JDK installation directory to the existing path variable, and the bin directory contains the frequently used executables such as Javac/java/javadoc wait, and after setting the path variable, you can execute the Javac/java tool in any directory.
2. CLASSPATH environment variables. The role is to specify the class search path, to use the already written classes, the premise of course is to find them, the JVM is through Classpath to find the class of the. class file. We need to set the Dt.jar and Tools.jar in the Lib subdirectory of the JDK installation directory to classpath, of course, the current directory "." must also be added to the variable.
Javac-c Path (You can specify a class file to store the directory)
JAVA-CP Path (You can specify the class directory to execute, such as JAVA-CP C:\Test HelloWorld)
3. Java_home environment variables. It points to the JDK's installation directory, and software such as Eclipse/netbeans/tomcat finds and uses the installed JDK by searching for java_home variables.
Hope to be helpful to beginners.
1. Path environment variable. The function is to specify the command search path, execute the command below the command line, such as Javac compiling the Java program, it will look in the path specified by the paths variable to see if the corresponding command program can be found. We need to add the bin directory in the JDK installation directory to the existing path variable, and the bin directory contains the frequently used executables such as Javac/java/javadoc wait, and after setting the path variable, you can execute the Javac/java tool in any directory.
2. CLASSPATH environment variables. The role is to specify the class search path, to use the already written classes, the premise of course is to find them, the JVM is through Classpath to find the class of the. class file. We need to set the Dt.jar and Tools.jar in the Lib subdirectory of the JDK installation directory to classpath, of course, the current directory "." must also be added to the variable.
Javac-c Path (You can specify a class file to store the directory)
JAVA-CP Path (You can specify the class directory to execute)
3. Java_home environment variables. It points to the JDK's installation directory, and software such as Eclipse/netbeans/tomcat finds and uses the installed JDK by searching for java_home variables.
JAVA environment variables Path, CLASSPATH, Java_home