CLASSPATH and environment variable settings

Source: Internet
Author: User

Http://www.360doc.com/content/12/0722/14/820209_225797366.shtml


Before setting the environment variables, we can run our programs by adding relevant information directly to the application. For example, we can start running a Java program like this:
C:/jdk1.3.1/bin/java-jar C:/windows/desktop/myfiles/simplecolorconverter.jar

This is certainly no mistake, every time we have to knock on the full path of the Java application and the full path of the class file, we write once no problem, two times do not feel long, three times also do not feel tired, but if we have to repeatedly knock on this pile of paths, it will really be a nightmare. So what can we do to reduce the amount of input we have?


First, Introduction:
Environment variables are the indication lights for operating systems, applications, scripts, and so on, to tell them where the resources are needed. Most systems have some pre-set environment variables, of course, we can also increase our own environment variables. To see the current system environment variables, we can use the following command: On the Linux/unix system, in the shell command entered Env, and then enter in the Windows system, at the DOS window prompt input set, and then enter this, We can see all the environment variables in the system, if we want to look at the value of a single variable.

On the Linux/unix system, enter the echo $ variable name in the shell command and return

In Windows system, enter the set variable or the echo% variable at the DOS window prompt, and then return

If you want to set an environment variable, you can use the following command:

On the Linux/unix system, enter the EXPort variable name = "Variable Value" in the shell command, and return

In the Windows system, enter the SET variable name = "Variable Value" at the DOS window prompt, and then return to the.

The values for the path and CLASSPATH environment variables are a list of sequential directories. Tell the system or the application where to find the resources they need. The list of directories is separated by a series of delimiters, and the delimiter in the Linux/unix system is a colon ":" is a semicolon in the Windows system; If we want to add some of our own values to an existing environment variable value, we can do this:

Linux/unix system: Export classpath= $CLASSPATH:/path/to/program

Windows system: Set CLASSPATH =%classpath%;c:/path/to/program


Second, set up the Java environment

To facilitate the development and running of Java applications, we need to set two environment variables, one path a classpath.

Set the path variable so that we can run Java applications anywhere in the system, such as Javac, Java, Javah, etc.

This is to find the directory where we install JDK, for example, our JDK is installed in the C:/JDK1.3/directory, then in the C:/jdk1.3/bin directory is our common Java application,

We need to add the C:/jdk1.3/bin directory to the PATH environment variable.

The CLASSPATH environment variable is when we need to refer to a class written by someone else when we are developing a Java program, let the Java interpreter know where to look for the class. Usually, Sun provides us with some extra rich class packs, one is Dt.jar, the other is Tools.jar, all two jar packages are located in the C:/jdk1.3/lib directory, so we usually add these two jar packs to our CLASSPATH environment variables

Set classpath=.; C:/jdk1.3/lib/tools.jar;c:/jdk1.3/lib/dt.jar. Focus on the jar package, in the classpath need to keep up with the full file path, and not just with a directory. The point "." of the first path represents the current directory, so that when we run Java AClass, the system will first look for aclass files in the current directory.

There are 3 main environment variables used in Windows JAVA, Java_home, CLASSPATH, PATH.
Java_home is pointing to the JDK installation path, such as c:/jdk_1.4.2, where you should be able to find bin, Lib and other directories. It is worth mentioning that the JDK installation path can choose any disk directory, but it is recommended that you put the level of the directory shallow, if you put a very deep directory, such as c:/xxxxxx/xxxxx/xxxx/xxxx/xxxx/xxxx/xxxx/xxx ...
To set the method:
java_home=c:/jdk_1.4.2
The PATH environment variable is originally in Windows and you just need to modify it to point to the JDK's Bin directory so that you don't need to type a bunch of paths when you compile and execute the program under the console. The set method is to keep the contents of the original path and add%java_home%/bin to it (note, if you do not know about DOS batches, you may not understand what the percent-percent content means; in fact, this is the reference to the previous set of environment variables Java_home, you write c:/ jdk_1.4.2 is also possible; you can open a console window and enter Echo%java_home% to look at your setup results:
path=%java_home%/bin;%path%
Again,%path% is a reference to the PATH environment variable you set up before, so you can copy the previous value.
CLASSPATH environment variable I put it on the last side, because you come up with a strange weird problem more than 80% may be due to the classpath set wrong, so be extra careful to do.
Classpath=.; %java_home%/lib/tools.jar
The first thing to pay attention to is the front of the ".;", if you can't see clearly, I'll read to you--a period semicolon. This is to tell the JDK, search the class first look at the current directory of the class file-Why do this, this is due to Linux security mechanism, Linux users understand that Windows users are very difficult to understand (because Windows default search order is to search the current directory , and then search the system directory, and then search the PATH environment variables set, so if you like Pangen friends may wish to study Linux.
Why Tools.jar This specific file was specified after classpath. Do not specify a line. Obviously not, I can do that. :) This is determined by the import mechanism of the Java language and the jar mechanism, and you can look up the data to solve it.
Specific settings: WIN2K/XP user right click on my Computer-> properties-> Advanced-> Environment variables, modify the following system variables in the box of the value of the line.
Win9x user modifies Autoexec.bat file and adds at the end:
Set java_home=c:/jdk_1.4.2
Set path=%java_home%/bin;%path%
Set classpath=.; Analysis of Java_home and classpath in%java_home%/lib/tools.jar JAVA 2009-10-26 10:0945

A lot of people in the beginner Java often will be introduced in the book of the set of environment variables to get dizzy, a lot of books will be installed in the JDK when he set java_home environment variables, in the development of the program when setting CLASSPATH environment variables, And many people do not understand the role of these two environmental variables, we have to elaborate separately. The first is Java_home environment variables, we first grasp the setting of this environment variable, java_home the setting of this environment variable is the JDK installation directory, for example, your JDK installed in d:/ jdk1.6.0 This directory, please check whether there is a subdirectory in this directory Bin,bin directory is not java.exe this file, if so, then your JAVA_HOME environment variable content should be d:/jdk1.6.0. So what is the role of setting this environment variable? In the case of individual Java development, this environment variable does not have any effect, this is a lot of according to the description of the book, after the completion of a book did not use this variable, so it is not understood. In fact, the Java_home variable is best set, first of all, in order to be able to compile and run Java programs, You need to set the directory where Java.exe resides as part of the path variable in the PATH environment variable in order to be able to run Javac and Java commands from the command line, when you enter javac on the command line ... or Java ... , if you are prompted with an error: ' Javac ' is not an internal command or an external command, nor is it a running program or a batch file. This explains why the operating system did not find the Javac command you entered through the command line. Because the operating system does not know where your javac.exe this file is, you cannot execute the corresponding file. So in order for the operating system to find this executable, we need to set up the directory where the Javac.exe executable file is located as part of the PATH environment variable, so that when you enter a command at the command line, the operating system automatically searches for all directories specified in the PATH variable. Out.

All paths in the path variable in the Windows system are separated by semicolons, if separated by colons in the Linux system. In addition, if you set the environment variable, in the command line or the same error, then there are two possibilities, one is that your settings are not correct, the other is your settings do not take effect, generally you can close the Command line window and then reopen on it. For this setting, take the directory just now as an example, then the content in the path variable is ...; D:/jdk1.6.0/bin, this time we can borrow the set of Java_home, modify the contents of the path to: ...; %java_home%/bin, this setting has some advantages, when you reinstall the JDK in your system, and change the directory, you only need to modify the contents of the Java_home, path is not required to modify. In addition, there are other uses for the java_home variable, such as the Eclipse IDE, which is itself written by a JAVA program, that requires the presence of a virtual machine at run time, so eclipse will need to find the virtual machine at startup, if there is a java_home variable , then eclipse will use this variable to find the path to the virtual machine. So although the beginning of the JAVA_HOME environment variable can not be set, but it is recommended for future consideration and set up. We continue to talk about classpath issues, a lot of books on the Java program when we recommend that you set CLASSPATH this environment variable, we do not recommend that you set. The reason we will see later on. Speaking of Classpath, let's first look at the two commands Javac and Java that have been used to write Java programs. Javac's command syntax is this: Javac < options > < source files > Our common situation is this: Javac-classpath < Classpath > < To compile Java source file > Here's the classpath input. The classpath here refers to classes in the non-Java base API used in the Java source file to be compiled in that directory where the path can be multiple directories separated by semicolons. For example, we're going to compile App.java this file, and this file uses the MyDate class in the Com.util package, and now we assume that mydate this class exists in the directory: d:/mylib/com/util/ Mydate.class, so how do we compile the app class? Our orders are as follows: Javac-classpath D:/mylib App.java attention to the contents of the classpath here, we just meanThe fixed path to the package directory cannot be specified with package, and Mydate.class must be in Com/util directory because this class defines package as Com.util. All right, now, do you understand the classpath in Javac? Let's take a look at the Java command, the syntax of the command is this: Java [option] class [parameters] For example, in the above example, we assume that the App class is located in the package of Com.app, and its directory is: d:/dev/myapp/com/app/ App.class, then how do we run this class? Commands are 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, This is the directory where the package of the app is located and the package of the class MyDate, where the classpath is given a directory of the classes to run and the package of all classes used by the class, separated by semicolons, You must add package when you finally specify a class to run. Well, you should have mastered the know-how of compiling Java programs and running Java programs here. If we set the CLASSPATH in the system environment variable, if you do not specify the CLASSPATH parameter when running through Java on the command line, then the Java Virtual machine will only search the class according to the directory in the CLASSPATH environment variable, not your current Directory to search the class, the result will naturally appear exception in thread "main" Java.lang.NoClassDefFoundError, so when you see this error, check your system environment variables, and if you don't have the classpath set up, examine your current directory. To ensure that it is foolproof, there is no error in following the example described above. Finally, I wish to meet the trouble of this kind of friends to solve the problem at an early date.

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.