what is the reason for the existence of the JAVA_HOME environment variable?

戏子无情 提交于 2019-11-26 12:39:46

问题


Many java based application requires to set JAVA_HOME env variable. What\'s the purpose of this variable?


回答1:


Environment variables are strings that contain information such as drive, path, or file name.

The JAVA_HOME environment variable points to the directory where the Java runtime environment (JRE) is installed on your computer.




回答2:


The purpose is to point to where Java is installed. $JAVA_HOME/bin/java should execute the Java runtime.




回答3:


When you run a Java program you need to first start the JVM, typically this is done by running an executable, on Windows this is java.exe. You can get that in lots of ways for example just giving a full path:

C:\somedir\bin\java.exe

or may having it on your PATH.

You specify as command line arguments some class files or Jar files containing class files which are your program. But that's not enough, the java.exe itself needs various other resources, not least all the Java libraries. By setting the environment variable JAVA_HOME you specify where the JRE, and hence all those runtime resources, are to be found. You should ensure that the particular Java you execute matches the setting of JAVA_HOME.

You may also need to put JAVA_HOME\bin early on your path so that if the java.exe spawns a copy of itself (for example to compile a JSP) it picks up the right version.




回答4:


JAVA_HOME is an environment variable.

It has to be setup on different operating system. Check out the following links.

  • JAVA_HOME in Ubuntu
  • JAVA_HOME in Windowx Xp
  • JAVA_HOME in Linux Bash
  • JAVA_HOME in Windows 7

Also see here for more information.




回答5:


JAVA_HOME allows you to run any of multiple java installations. Even if you have the /usr/lib/jvm/java-1.8.0/bin/java in your PATH first, it's useful to be able to say JAVA_HOME=/usr/lib/jvm/java-1.5.0 and have the default "java" with which you run programs use the other one, instead, as a matter of an environment setting. This allows a programmer to use the java they need, even if it's not installed in a normal system location, and test or provide reverse compatibility with that alternate version. And it's not vulnerable to being reset in the PATH by another installed program, such as a major version update for java itself.




回答6:


According to @dirai here JAVA_HOME is not used by Java itself but by some third party tools like for instance Apache Tomcat. Most applications however will work without this variable being set but some could show unexpected behaviour.




回答7:


JAVA_HOME is used to set an environment that will enable you run java programs on your machine or pc after installing the JDK(Java Development Kit)



来源:https://stackoverflow.com/questions/5102022/what-is-the-reason-for-the-existence-of-the-java-home-environment-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!