Why should JAVA set environment variables, java_home

Source: Internet
Author: User

1. Why do you want to say this question?

Think of two years ago just learning Java, was asked to set the environment variables, naturally puzzled, then find the answer online.

Now I want to feel that the answer I saw at the time was God-God, vague, not a few said plainly. Of course, it could be that I didn't see it.

In short, believe me, read my blog, you don't have to find another place!

2. Environment variables

environment variable is English literal translation:environment Variable .

    1. variable , you know?
      Yes, it is a storage unit that can be assigned to its value arbitrarily.

    2. What about the environment ?

      First of all, you think of the operating system as the largest software, other software such as Office, LOL, JVM and so on are running in this software small software.
      Then, the environment is the operating system software.

    3. So what's the "variables in the environment" together?
      Don't tease me. The environment variable is the variable in the software of the operating system, namely: global variable !

    4. Role?
      Environment variables are nothing special, the role of variables in any program is assigned/used! This global variable operating system can be used, its small software can also be used!

3. What kind of environment variables are long? What are they?

What is the variable, the environment variable is what kind ...

xxx = xxx

1. In Windows:

Here is WIN10, such as ↓


Of course, Windows is a Windows system, there is no explicit way to give xxx=xxx, but directly can click on the "edit" operation.

2. In Linux:
Set in the /etc/profile file.
For example, there is a line of code that

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

The meaning of export is to set the environment variable (local variable), which can also be understood as the "global variable" mentioned above. There are some default values for these environment variables, and of course we can add them, for example:

# : 的意思是并列PATH = /abc/def:$PATH
4. In Java

Know the role of environmental variables. So, what are the specific uses in Java? What is their role?

    • One thing you need to understand first is that environment variables are largely for the direct upper-level programs of the operating system. such as Linux shell , .sh files, Windows cmd , .bat files.

Common Java-related environment variables and functions

  1. PATH

    This is actually the operating system used to specify the operating system needs to use the location of the program. For Java, it is often necessary to add the bin directory of the JDK into the directory PATH so that you can use the bin, and so on, in any of the directories javac.exe java.exe .

    This variable must be guaranteed to jre/jdk bin into it (here I say is not rigorous, you can javaw.exe copy such files and so on any PATH path, not necessarily jre/jdk), So many Java programs can be started, such as Eclipse .

  2. JAVA_HOME

    JAVA_HOMEis a convention, usually it refers to the directory of the JDK. If you need a JDK, most programs will default to the environment variable JAVA_HOME .

    For example, the Tomcat .bat/.sh file will be used by default JAVA_HOME .

    So, this variable needs to be set most. Set it, can keep you safe, life and peace ...

  3. JRE_HOME

    Again, this is an agreed variable, usually referred to as the JRE directory. In fact, most Java programs do not need the JDK, but only the subset of the JRE, so many programs will take this variable to use.

    For example, in some .bat files in tomcat this says:

    rem   JAVA_HOME       Must point at your Java Development Kit installation.rem                   Usingas well.remrem   JRE_HOME        Must point at your Java Runtime installation.rem                   toifIfand JAVA_HOMErem                   setis used.

    The idea is: Tomcat to take these two variables, as long as any one is not empty on the line, are not empty on the use of jre_home. Because Tomcat doesn't need the tools in the JDK, it just runs Java programs.

    This variable, if you know for sure that the application only needs to set this line (like the one above Tomcat), then it can replace Java_home, and it's best to use this.

  4. CLASSPATH

    Basically, this variable refers to the AppClassLoader class path that is loaded. (This involves the Java class loading mechanism, everyone first to understand and then will understand).

    This variable is related to all Java-written programs, not just other Java programs (such as Tomcat used in some cases), and the Java program we write is also available. But in fact ..., if you use it Eclipse you just need to click on the BuildPath line, or not let you explicitly in XXX=XXX this way. But it Command Line 's usually used if you run Java programs set CLASSPATH .

    You can get the current content in the Java program with the code CLASSPATH , like this:

    System.getProperty("java.class.path")//输出是String内容是你项目的class文件的根目录 + 你引用的jar包们的地址。类似这样E:\test1\target\classes;E:\Java\Program Files\.m2\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar...

    So, this variable is usually not required to be manually set up with the IDE, just follow the general IDE steps (e.g. Build Path ) the IDE will do it for you.

  5. CATALINA_HOME
    This is used by Tomcat. Name Catalina, I guess because the name of the Tomcat default service (also <Service></Service> labeled) is Catalina. Of course, whether it is not really unimportant, after all, the name of the variable can be random (but the best known), it is important to what value it. Its value represents the current Tomat root directory, for example: E:\apache-tomcat-7.0.55 .

    this variable only needs to be specified when you move the file that you are using .bat/.sh to start Tomcat (which is not in the original tomcat\bin ).
    Specific scenarios, such as in Linux, you will catalina.sh /etc/rc.d/init.d/ need to set up when you copy the service from the start.

Summarized as follows:

1. Variables that need to be set under the operating system:

    1. PATHContains the directory of JRE/JDK bin ;
    2. JAVA_HOMEor JRE_HOME . All set also can, lazy just set JAVA_HOME on can.

2. Few settings are required:
CLASSPATH,CATALINA_HOME

Why should JAVA set environment variables, java_home

Related Article

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.