Many people are often overwhelmed by the setting of a bunch of environment variables introduced in the book when learning java. Many books will ask them to set java_home environment variables when installing JDK, in development ProgramWhen setting the classpath environment variable, many people do not understand the role of these two environment variables, We will elaborate in detail.
The first is the java_home environment variable. Let's first master the setting content of this environment variable. The setting content of java_home is the JDK installation directory. For example, if your JDK is installed on D: \ jdk1.6.0this directory. Please refer to this directory. There is no java.exe file in this directory. If yes, the content of your java_home environment variable should be D: \ jdk1.6.0. So what is the role of setting this environment variable? For Java development alone, this environment variable does not play any role. This is because many of the environment variables have been set based on the description in the book and are not used after learning a book, so I don't understand it very much.
The directory where the handler is located is set as part of the PATH variable to be able to run javac and Java commands through the command line. When you enter javac... or Java... if you are prompted with an error: 'javac' is not an internal or external command, or a program or batch file that can be run. At this time, the operating system does not find the javac command you entered through the command line. Why? Because the operating system does not know where your javac.exe file is, the corresponding file cannot be executed. So that the operating system can find this executable file, we just set the directory of the javac.exe executable file as part of the PATH environment variable, so that when you enter a command in the command line, the operating system automatically searches all Directories specified in the PATH variable. In Windows, all paths in the PATH variable are separated by semicolons (;). in Linux, colon (:) is used. In addition, if you have set the environment variables and the same error still occurs in the command line, there are two possible reasons: your settings are incorrect, the other is that your settings do not take effect. Generally, you can close the command line window and re-open it. This setting takes the directory as an example. The content in the PATH variable is ...; d: \ jdk1.6.0 \ bin. At this time, we can borrow the set java_home and change the path content :...; % java_home % \ bin. This setting has some advantages. When you reinstall JDK in your system and change the directory, you only need to modify the content of java_home, path does not need to be modified. In addition, the java_home variable has some other purposes, such as Eclipse, which is written by Java programs. Therefore, the existence of virtual machines is required during running, therefore, eclipse needs to find the virtual machine at startup. If the java_home variable exists, eclipse will use this variable to find the path of the virtual machine. Therefore, although the java_home environment variable can be left unspecified, we recommend that you set it for future consideration.
We will continue to discuss the classpath issue. Many books suggest you set the environment variable classpath when explaining Java programs. We do not recommend that you set it here. We will see the cause later. Speaking of classpath, Let's first look at the two commands javac and Java that have been used during Java programming.
The command syntax of javac is as follows:
Javac <option> <Source File>
We often use the following situation:
Javac-classpath <class path> <Java source file to be compiled>
What is the class path input here? The class path here refers to the class in the non-Java basic API used in the Java source file to be compiled under that directory. The path can be multiple directories separated by semicolons. For example, we want to compile the app. java file, which uses COM. the mydate class in the util package. Now we assume that the mydate class has a directory: D: \ mylib \ com \ util \ mydate. so how do we compile the app class? The command is as follows:
Javac-classpath D: \ mylib app. Java
Note that the content of classpath here is to specify the path to the directory where the package is located. It is not allowed to specify the package together, And mydate. the class must be in the com \ util directory because the class defines package as COM. util. Now, do you understand the classpath in javac?
Let's take a look at the Java command. the syntax of this command is as follows:
Java [Options] class [parameters]
For example, in the above example, we assume that the app class is located at com. in the package of APP, its storage directory is: D: \ Dev \ MyApp \ com \ app. class, how do we run this class? The command is as follows:
Java-classpath D: \ Dev \ MyApp; D: \ mylib com. App. app
From the command, we can see that after classpath, we specify two directories: the package directory of the app class and the package directory of the mydate class, the class path here should be given the directory of the class to be run and the package of all classes used by this class. Separate multiple directories with semicolons, the package must be added when the class to be run is specified.
Now you have mastered how to compile and run Java programs. If classpath is set in the system environment variable, if the classpath parameter is not specified when running through Java in the command line, therefore, the Java Virtual Machine will only follow the Directory Search Class in the classpath environment variable, instead of the Search Class in your current directory. The result will naturally be exception in thread "Main" Java. lang. noclassdeffounderror, so when you see this error, check your system environment variables. If classpath is not set, check your current directory. To ensure that nothing is lost, follow the example above to perform operations without any errors.
Finally, I wish my friends who have encountered such troubles can solve the problem as soon as possible!
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/stevencn76/archive/2007/09/13/1784272.aspx
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service