简体   繁体   English

我需要在 MacOS 上设置 JAVA_HOME 吗?

[英]Do I need to set JAVA_HOME on MacOS?

I'm fairly new to Java.我对Java相当陌生。 Many of the tutorials on installing Java on a Mac recommend setting up the JAVA_HOME environment variable.许多关于在 Mac 上安装 Java 的教程都推荐设置JAVA_HOME环境变量。 However, Java works fine on my computer with the JAVA_HOME variable being empty!但是,Java 在我的计算机上运行良好, JAVA_HOME变量为空!

I have successfully used Java from the command line (using javac and java ), in IntelliJ IDEA (there in the "Project Structure Settings" I've set up the "JDK home path" to /Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home) and in Sublime Text to compile and run Java files.我已经成功地从命令行(使用javacjava )在IntelliJ IDEA 中使用了java (在“项目结构设置”中,我已经将“JDK 主路径”设置为/Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home)Sublime Text来编译和运行 Java 文件。

So, my questions are:所以,我的问题是:

  • Why does Java work without the JAVA_HOME on my Mac?为什么在我的 Mac 上没有JAVA_HOME情况下 Java 也能工作?
  • Do I need to set up JAVA_HOME at all?我需要设置JAVA_HOME吗? What would be the consequences of not having it?没有它会有什么后果? Maybe it is not essential for some things, but is required for other things to work?也许它对某些事情不是必需的,但对于其他事情来说是必需的? Then what are these things?那么这些东西是什么呢?
  • Is this behavior different on other operating systems?这种行为在其他操作系统上是否有所不同?

Outputs of some commands are:一些命令的输出是:

echo $JAVA_HOME outputs an empty line echo $JAVA_HOME输出一个空行

echo $CPPFLAGS outputs an empty line echo $CPPFLAGS输出一个空行

which java

/usr/bin/java

echo $PATH contains /usr/bin echo $PATH包含/usr/bin

Why does Java work without the JAVA_HOME on my Mac?为什么在我的 Mac 上没有 JAVA_HOME 的情况下 Java 也能工作?

Setting JAVA_HOME is not required for Java to work in your system. Java 在您的系统中工作不需要设置JAVA_HOME Some applications (eg Tomcat, Maven etc.) however look for JAVA_HOME system variable and if its value is not set, they may prompt you to do so.然而,某些应用程序(例如 Tomcat、Maven 等)会查找JAVA_HOME系统变量,如果未设置其值,它们可能会提示您这样做。

Do I need to set up JAVA_HOME at all?我需要设置 JAVA_HOME 吗? What would be the consequences of not having it?没有它会有什么后果? Maybe it is not essential for some things, but is required for other things to work?也许它对某些事情不是必需的,但对于其他事情来说是必需的? Then what are these things?那么这些东西是什么呢?

Already answered above.上面已经回答了。

Is this behavior different on other operating systems?这种行为在其他操作系统上是否有所不同?

It's same across all operating systems.所有操作系统都一样。

It depends on the program.这取决于程序。

Most programs that depend on Java follow logic like this大多数依赖 Java 的程序都遵循这样的逻辑

if $JAVA_HOME ; then
  use JAVA_HOME/bin/java
elif $(which java); then
  use java command on PATH
else
  error
fi

That being said, to be consistent , you should set it , and using SDKman or Homebrew does that for you话虽如此,为了保持一致,您应该设置它,并使用SDKman或 Homebrew 为您做到这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM