In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of what Java Path is, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Java Path article. Let's take a look at it.
Typically, we need to set three environment variables: Java_HOME, PATH, and CLASSPATH.
JAVA_HOME: the value of this environment variable is the directory where Java is located. Some Java software and some Java tools need to use this variable. When setting PATH and CLASSPATH, you can also use this variable to facilitate setting.
PATH: specifies a list of paths to search for executable files. When executing an executable file, if the file cannot be found under the current path, look for each path in the PATH in turn until it is found. Or if the path in the PATH cannot be found, an error will be reported. Java's compilation commands (javac), execution commands (java), and some tool commands (javadoc, jdb, etc.) are all in the bin directory under its installation path. So we should add the path to the PATH variable.
CLASSPATH: also specify a list of paths that are used to search for classes needed to compile or run Java. You can include .jar files in the CLASSPATH list in addition to paths. When Java looks for classes, it looks for the .jar file as if it were a directory. Typically, we need to include the jrelibrt.jar (Linux: jre/lib/rt.jar) under the jdk installation path in the CLASSPATH.
Both PATH and CLASSPATH specify a list of paths, and the items in the list (that is, paths) are separated by delimiters. Under windows, the delimiter is a semicolon (;), while under Linux, the delimiter is a colon (:).
The following shows how the three environment variables are set under Windows and Linux, respectively, but before that, we need to make an assumption. Suppose the installation path for JDK under Windows is C:jdk, and the installation path for Linux is / usr/local/jdk/. Then, the installed JDK will at least include the following:
C:jdk (/ usr/local/jdk)
|-- bin
|-- demo
|-- include
|-- jre
| |-- bin |
| | `--lib |
`--lib
* set under Windows
Use the set command to set environment variables under Windows. In order to set these environment variables every time you start the computer, you should set them in the autoexec.bat file under the root directory of the system disk, such as:
Set JAVA_HOME=C:jdk
Set PATH=%JAVA_HOME%bin;C:Windows;C:WindowsCommand
Set CLASSPATH=%JAVA_HOME%jrelibrt.jar;.
Some versions of Windows cannot replace the contents of environment variables with% variable name%, so you have to write C:jdk instead of% JAVA_HOME%. In addition, C:Windows and C:WindowsCommand are automatically added to the path by Windows, so they can be removed from the settings. If PATH is already set in autoexec.bat, you just need to add% JAVA_HOME%bin to the statement that originally set PATH.
CLASSPATH can also be set up or added to other paths as needed. For example, if you want to put some of the classes you write in C:java, you can add C:java to CLASSPATH, set CLASSPATH=%JAVA_HOME%jrelibrt.jar;C:java;..
Notice that a "current directory (.)" is included in CLASSPATH. Once the directory is included, you can go to any directory to execute Java programs that need to use a class in that directory, even if the path is not included in the CLASSPATH. The reason is simple: although the path is not explicitly included in CLASSPATH, the "." in CLASSPATH. This represents the path at this time, such as:
Assuming that there is a runnable class HelloJava.class in the C:java directory, then
C: > set CLASSPATH=C:jdkjrelibrt.jar;. / / set the CLASSPATH environment variable, and notice that there is a "."
C: > cd java / / go to the C:java directory
C:java > java HelloJava / / run HelloJava
Hello, Java. / / Operation result
C:java > _
This example will be analyzed in Section 1.3.3.
* set under Linux
Under Linux, use "variable name = variable value" to set the variable and use the export command to export it as an environment variable. In order to set these variables automatically for each login, you need to set them in ~ / .bash_profile or ~. / bashrc, such as
Export JAVA_HOME=/usr/local/jdk
Export PATH=$JAVA_HOME/bin:$PATH
Export CLASSPATH=$JAVA_HOME/jre/lib/rt.jar:.
The $JAVA_HOME used when setting up PATH refers to replacing the value of the variable JAVA_HOME to the location of $JAVA_HOME. The above sentence is actually export PATH=/usr/local/jdk/bin:$PATH. The same is true of $PATH in this sentence, except that PATH refers to the value of the previously set PATH variable, not the value of the PATH variable set this time.
Notice that a "current directory (.)" is included in CLASSPATH. Once the directory is included, you can go to any directory to execute Java programs that need to use a class in that directory, even if the path is not included in the CLASSPATH. The reason is simple: although the path is not explicitly included in CLASSPATH, the "." in CLASSPATH. This represents the path at this point, for example
Assuming that there is a runnable class HelloJava.class in the / home/fancy/java directory, then
[fancy@matrix fancy] $export CLASSPATH=/usr/local/jdk/jre/lib/rt.jar:. / / set CLASSPATH, pay attention to the last "."
[fancy@matrix fancy] $cd ~ / java / / go to / home/fancy/java
[fancy@matrix java] $pwd / / Show the current directory
/ home/fancy/java / / the current directory is / home/fancy/java
[fancy@matrix java] $java HelloJava / / run HelloJava
Hello, Java / / run result
[fancy@matrix java] $_
This example will be analyzed in Section 1.3.3
* case analysis
The examples used in sections 1.3.1 and 1.3.2 are actually the same, except that the operating systems are slightly different.
Both examples refer to a "runnable class", which refers to a class that contains a public static void main (String [] args) method, which is detailed in the next chapter, HelloJava. None of the CLASSPATH in the example contains the directory where the HelloJava.class is located (C:java, / home/fancy/java), but they all contain the current directory (.). So go to the directory that contains HelloJava.class and execute java HelloJava. When Java looks for ". (current directory, C:java, / home/fancy/java)" in CLASSPATH, it finds HelloJava.class and runs successfully.
This is the end of the article on "what is Java Path?" Thank you for reading! I believe you all have a certain understanding of "what is Java Path". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.