What is the purpose of operating system variables related to Java?

Asked

Viewed 381 times

0

There are environment variables that Java uses to perform certain actions, such as JAVA_HOME, CLASSPATH, Path, ... Most of the material talking about these variables are confusing, often some information is left out or one source belies the other, so it is difficult to know the real purpose of the variables.

So I wanted to know what these variables are for and what value should I assign to them?

  • 2

    The negative personnel need to remember to come and remove the negative after the question is improved... :) +1

1 answer

2


I will give examples on Windows, but all that changes are the folder paths.

Environment variables in general are used as a way to configure some application in a simpler way. Java looks for this information in the operating system to get some specific data, such as an executable file.

Java can use the following environment variables: JAVA_HOME, CLASSPATH, Path, JDK_HOME and JRE_HOME, JAVA_TOOOL_OPTIONS, JDK_JAVA_OPTIONS, _JAVA_OPTIONS and JAVA_OPTS.

Note that it is not necessary to have all these variables set to run Java, most of the time only JAVA_HOME, CLASSPATH and Path are used.

  • JAVA_HOME:

    • It points to the Java base folder (more precisely the JDK folder in case you have installed it, something like this: "C: Program Files Java jdk12.0.5_24", if you have not moved the Java folder from place at the time of installing. The same goes for the JRE).
    • This variable is used to flexibly change the value of the other variables when we update the Java version, just change the value of this variable. It can also be used by other programs to locate the Java folder.
  • CLASSPATH:

    • Targets third-party libraries, such as Javafx, which already comes within Java files but is not included in it.
    • The value of this variable has to be some things that are not included in Java as ". ;%JAVA_HOME% lib;%JAVA_HOME% jre lib;". Note that "." is the current folder we are in (at the time of compiling a file in Java), the ";’s" separate the paths and finally "%JAVA_HOME%" is taking the value of the variable JAVA_HOME and putting there.
    • Using an example with Javafx the value of this variable would have what I mentioned above and: "...;%JAVA_HOME% jre lib jfxswt.jar;". The file I specified contains all Javafx packages compressed into a file. jar.
  • Path:

    • This is a widely used variable by the system.
    • It points to a folder that contains files that execute certain commands written on the command line, such as javac, java, jshell, jjs, jvisualvm, among others.
    • The value of this variable must be: "...;%JAVA_HOME% bin". Note that this variable may have other values already predefined in it.
  • JDK_HOME and JRE_HOME:

    • These folders simply point to the JDK or JRE folder.
    • If you downloaded the JDK, in the JRE_HOME variable put the value: "%JAVA_HOME% jre". Otherwise, put only "%JAVA_HOME%". Do not set JDK_HOME if you installed JRE.
    • In JDK_HOME just put as value "%JAVA_HOME%".
  • JAVA_TOOL_OPTIONS and JDK_JAVA_OPTIONS (Java 9+):

    • These variables configure certain things of java when interpreting and compiling some file. java, however there is a certain difference.
    • JDK_JAVA_OPTIONS is only used by the command java.
    • JAVA_TOOL_OPTIONS is also used by other commands like jar and javac.
    • As there are several, I recommend that you search in the documentation the values of these variables.
  • _JAVA_OPTIONS and JAVA_OPTS:

    • JAVA_OPTS should be ignored and no value, it is not used by JVM.
    • _JAVA_OPTIONS is undocumented, it does the same thing as JAVA_TOOL_OPTIONS. This variable is above the command line arguments, and JAVA_TOOL_OPTIONS is above this variable.
  • 2

    path separator in windows is semicolon ; but on linux and mac-os is two-point :

  • 1

    Nice that clarified to which variables you were referring, but the place of this clarification is in the question and not in the area of answers.

  • 1

    you have not explained the essential, about environment variables, they serve for initial settings of running jvm, are information that java search the operating system, this serves not only for the default variables that you mentioned, but also for custom variables, so your applications can run differently depending on the environment, depending on the computer.

  • 2

    They are not Java environment variables; they are operating system environment variables that are related to Java. Java has other environment variables, obtained with System.getProperty() user.dir, os.name, line.separator... so the question got negative (correctly).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.