Java environment variable configuration problems

Source: Internet
Author: User
Tags bootstrap classes

When I wrote the third article "build a JAVA Development Environment-EditPlus" last night, after configuring the CodeGenerator plug-in, I found that the class was always not found when I executed the java command in the command line, i'm sure the path is correct.) This problem was also encountered on the company's computer after the CodeGenerator plug-in was configured.) At that time, I thought it was a problem with JDK, but I didn't want to reinstall it. This problem occurs again on my computer, which means it is not a problem with JDK. So where is the problem? I remember that when I first started learning JAVA, many books, videos, and teachers will explain how to set java environment variables. After installing Java, you usually need to configure several environment variables: JAVA_HOME, PATH, and CLASSPATH. At that time, according to what they said, I didn't know much about the reason or their role. I think most people started learning this kind of situation, the explanation is generally not explained by anyone. Today, I encountered problems related to environment variables, so I will summarize these issues.1. Notes for Java installationWhen installing Java, we recommend that you do not install JDK in a path with spaces, such as Program Files. Why do we recommend this? The book or instructor's statement is: such other software that needs to use Java may have inexplicable problems. It is not clear why "inexplicable problems" occur. In fact, this problem is quite understandable. In the command line, commands, options, parameters, and so on are separated by spaces. If the javac/java command is referenced by other software, the full path is usually used.) The path contains spaces, the front part of the space will be treated as a command, and obviously there will be problems. In fact, in the class file path, if it contains spaces, during compilation or running, this path needs to be placed in quotation marks, otherwise it will be processed as two parts.2 Java environment variable configuration should be notedAfter installing Java, you can also use it without configuring environment variables. The full path is required to use Java-related commands in the command line. However, this is inconvenient, and software written in Java such as Eclipse cannot run. Therefore, it is necessary to configure necessary environment variables.2.1 JAVA_HOME Environment VariablesThe configuration process is not mentioned. It mainly describes the role of the variable. In fact, Java itself does not need JAVA_HOME environment variables. This environment variable is only used for other software written by Java) to find the Java installation path. For example, Ant needs to configure the JAVA_HOME environment variable first.2.2 PATH Environment VariablesThis variable is configured to facilitate the use of Java-related commands without entering the complete path. You only need to enter the command name.2.3 CLASSPATH Environment VariablesMany tutorials and even teachers have said they want to configure this environment variable and add rt. jar and tools. jar to it. Actually, it is not necessary. These classes are loaded by the JAVA startup Class Loader boow.classloader, and these are the startup class Bootstrap classes ). Here, I will explain how to find class files when JAVA is running? (The following is from 51cto "http://developer.51cto.com/art/200907/135263.htm") according to the JDK documentation, JAVA programs search for running class files in the following three sequence. ◆ Bootstrap classes (*) ◆ Extension classes ◆ Users classes2.3.1 Bootstrap classesIt is the class files loaded by JAVA at startup. These class files are mainly class files under the rt. jar and jre/lib directories. The class path in the Bootstrap process is stored in the system attribute of sun. boot. class. path. You can use System. out. println (System. getProperty ("sun. boot. class. path. At the same time, Bootstrap classes can be specified through the-Xbootclasspath command line parameter. The default Bootstrap classes: jre \ lib \ rt are listed below. jar; jre \ lib \ i18n. jar; jre \ lib \ sunrsasign. jar; jre \ lib \ jsse. jar; jre \ lib \ jce. jar; jre \ lib \ charsets. jar; jre \ classes * Bootstrap bootstrap is a small strap or a small ring at the back of the shoes. It allows you to easily wear the shoes. In a computer, a very small program is used to load a specific program (usually an operating system) into a computer.2.3.2 Extension classes (Extension class files)Extension classes mainly refers to class files under the jre/lib/ext directory. These files must be in jar files or zip files. If the jar package of different names contains the same class file, it is uncertain which class file is loaded.2.3.3 User classes (User-defined class files)Now the most important thing is to specify the User classes. JAVA searches for User classes in the following order.2.3.3.1 the Default User classes. (dot) indicates the current directory. 2.3.3.2 set the specified class library as the system variable CLASSPATH, which overwrites the Default User classes. 2.3.3.3 use the class library specified by the command line parameter-cp or-classpath. In this case, override the Default User classes and CLASSPATH setting variables. 2.3.3.4 use the jar file specified by the-jar parameter. In this case, all the class files must come from the specified jar file.After learning how to find the User classes in java, we will learn how to set the CLASSPATH variable.2.3.4 configure the CLASSPATH environment variableIf you only use java, but only use the basic library files of java, you do not need to set CLASSPATH. However, in general development, this is rare. Therefore, we need to set our own CLASSPATH. The general method is to set the CLASSPATH system variable according to 2.3.3.2. Like starting to set the path, we can add a system variable set by CLASSPATH. Some articles on the Internet mentioned that adding rt. jar and tools. jar are unnecessary because these class libraries belong to Bootstrap classes. We only need to define the User classes, and if we want to compile the servlet, we only need the servlet-api.jar and jsp-api.jar to implement the basic needs. Note 2.3.3.2 override 2.3.3.1. When CLASSPATH is set, put. (dot) [indicates the current directory] In the CLASSPATH setting and separate it with; (semicolon. Because other provided class files are stored in jar files, we must include the complete jar file package in the CLASSPATH settings, instead of adding its directory to the CLASSPATH settings (important ). At this time, another problem occurs. when too many jar files need to be added, it is very troublesome to manage the java class library. Therefore, I think that using command line compilation and debugging in basic learning is conducive to JAVA learning, but IDE tools are usually used in development (eclipse and netbeans are currently popular ), or use Ant to compile.3. ConclusionAt the beginning of this article, the problem becomes clear: when CLASSPATH is not configured, there is no error, but the configuration is wrong, because 2.3.3.2 overwrites 2.3.3.1. Therefore, when configuring CLASSPATH, put ".;" at the beginning to search for the current directory first. JAVA environment variable configuration issues are mentioned here, I hope to help you. The class loader is a very important issue. I will write a series article on "Java Virtual Machine Learning" in the future, which will explain in detail the class loader.

This article is from the "Xu Xinhua polaris" blog, please be sure to keep this source http://polaris.blog.51cto.com/1146394/374280

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.