Parsing of Java_home and Classpath in Java (RPM)

Source: Internet
Author: User

Many people in the beginning of Java often will be introduced in the book of the set of environment variables to get dizzy, many books will be in the initial installation of the JDK let him set the Java_home environment variables, in the development of the program when set CLASSPATH environment variables, And many people do not understand the role of these two environmental variables, we have to elaborate separately.
The first is the JAVA_HOME environment variable, we first grasp the environment variable setting content, java_home this environment variable setting content is the JDK installation directory, for example, your JDK installed in D:\ jdk1.6.0 This directory, please check if 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 do we do with setting this environment variable? OnThis environment variable has no effect on individual Java development, this is a lot of according to the description of the book set up, after a book has not been used to learn the variable, so it is not understood.
In fact, the Java_home variable is best set, first of all, we are able to compile and run the Java program, You need to set the directory where the Java.exe resides in the PATH environment variable to be part of the path 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 external command, nor is it a program or batch file that can be run. At this point the operating system did not find the command line that you entered by the Javac, why? 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 the executable file, We need to set up the directory where the Javac.exe executable 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 the directories specified in the PATH variable. All paths in the path variable in the Windows system are separated by semicolons, if separated by colons in the Linux system. Another point, if you set the environment variables, the same error in the command line, then there are two possibilities, one is that your settings are not correct, the other is that your settings do not take effect, generally you can close the Command line window and then reopen it. For this setting, in the case of the directory just now, the contents of the path variable are ...; D:\jdk1.6.0\bin, at this time we can borrow the already set of Java_home, the content of path modified to: ...; %java_home%\bin, this setting has some advantages, when you reinstall the JDK in your system, and change the directory, you just modify the contents of Java_home, path is not required to modify. In addition, the Java_home variable will have some other uses,such as the Eclipse IDE, which itself is written by the Java program, it is necessary to run the existence of virtual machines, so eclipse at the start of the need to find the virtual machine, if there is java_home this variable, Then eclipse will use this variable to find the path to the virtual machine. So although the start Java_home environment variable can not be set, but it is recommended for future consideration and set up.
We continue to talk about classpath problems, many books on the Java program is recommended to set CLASSPATH This environment variable, we do not recommend that you set. Cause we'll see it later. Speaking of Classpath, let's start by looking at the two commands Javac and Java that have been used to write Java programs.
The command syntax for Javac is this:
Javac < options > < source files >
Here's what we used to do:
Javac-classpath < classpath > < Java source files to compile >
What does the Classpath enter here? Here'sA classpath is a class in a non-Java underlying API that is used in a Java source file to be compiled in that directory, where the path can be multiple directories, separated by semicolons. For example, we want to compile App.java this file, and this file uses the Com.util in this package mydate this class, now we assume that mydate this class exists directory is: d:\mylib\com\util\ Mydate.class, how do we compile the app class? Our order is as follows:
Javac-classpath D:\mylib App.java
Note the contents of the classpath here,We just specify the path to the directory where the package is located, and we cannot assign the package together., and Mydate.class must be in the Com\util directory, because this class defines the package as Com.util. All right, now you understand the classpath in Javac?
Let's look at the Java command again, the syntax for this command:
java [Options] class [parameters]
For example, let's say that the app class is in the Com.app package and its directory is: D:\dev\myapp\com\app\App.class, so how do we run this class? The command is as follows:
Java-classpath d:\dev\myapp;d:\mylib com.app.App
As we can see from the command, we specify two directories behind classpath, namely the directory where the package for the app is located and the directory where the package for this class mydate is located. The classpath here is given the directory where the class to run and the package for all classes used by this class are separated, separated by semicolons, and the package must be added when the class to run is finally specified.
Well, here you have a thorough grasp of the secret to compiling Java programs and running Java programs. If we set the CLASSPATH in the system environment variable, if we do not specify the CLASSPATH parameter when running through Java in the command line, then the Java Virtual machine will simply follow the directory search class in the CLASSPATH environment variable. Instead of searching for the class in your current directory, the result will naturally appear exception in thread "main" Java.lang.NoClassDefFoundError, so when you see this error, please check your system environment variables, if not set classpath, then check your current directory. To ensure foolproof, follow the example described above without any errors.
Finally wish to meet this kind of trouble friends to solve the problem at an early date!
This article transfers from CSDN blog, reprint please indicate source:http://blog.csdn.net/stevencn76/archive/2007/09/13/1784272.aspx

Parsing of Java_home and Classpath in Java (RPM)

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.