[英]How to know if environment variable was successfully set (Mac)
尝试将我的JAVA_HOME环境变量设置为JDK的位置。 ls -l 'which java'
返回lrwxr-xr-x 1 root wheel 74 Nov 9 13:31 /usr/bin/java -> /System/.../java
所以我假设/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
是我的JDK的正确位置。 所以我应该做的就是写export JAVA_HOME=$(/System/.../java)
。
这样做,我得到一个巨大的输出:
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the "server" VM
The default VM is server,
because you are running on a server-class machine.
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A : separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose:[class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
Warning: this feature is deprecated and will be removed
in a future release.
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
Warning: this feature is deprecated and will be removed
in a future release.
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions with specified granularity
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions with specified granularity
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
这看起来像是对java
命令的使用有所帮助。 我的出口成功与否? JAVA_HOME现在设置为我的JDK的位置吗?
首先,要使其永久化,您必须编辑mac中的~/.bash_profile
文件。 在该文件中,键入上面的导出命令。
要检查它是否成功,请在终端中键入echo $JAVA_HOME
并查看输出。 这将告诉您为$ JAVA_HOME设置当前环境变量的内容。
此外, which
命令只查找java
的可执行文件,而不是SDK的存储位置。 您需要找到计算机上安装java的位置。 对我来说,我的是这样的:
export JAVA_HOME=$(/usr/libexec/java_home)
这将在安装新版本的Java时自动更新。
要了解您的JAVA_HOME应该是什么,您应该能够从终端执行:
/usr/libexec/java_home
如果您已安装Oracle的Java并在系统首选项中安装了Java applet,则此应用程序将返回用作JAVA_HOME的正确路径。
您可以使用返回的原始路径将其添加到.bash_profile文件中,或者可以通过某些方法执行命令并将std-out响应分配给变量。 例如
export JAVA_HOME=$(/usr/libexec/java_home)
我在我的系统上有一些应用程序(UnrealEngine),它们查看原始环境,因此不了解JAVA_HOME这样设置,所以我不得不硬编码。
如果以上内容不适用,请注意JAVA_HOME必须不是指向java可执行文件,而不是指向包含java可执行文件的文件夹,而是指向一个文件夹,这样$JAVA_HOME/bin/java
就是$JAVA_HOME/bin/java
的路径。 如果您已正确设置,则可以执行
$JAVA_HOME/bin/java -version
看看你的java版本。
请注意,JAVA_HOME应该是默认java安装的基本文件夹的路径。
'/ usr / libexec / java_home'不是(或者不应该)是java安装的路径 - 甚至是符号链接 - 它是一个通过stdout返回的程序,stdout是配置的默认java安装的路径。
因此,您应该将其包装在反引号或$()中,因为这会导致bash执行括号内容,并将其stdout分配给JAVA_HOME。
至少,这是Oracle JDK在OSX上设置的方式。 如果export JAVA_HOME=/usr/libexec/java_home
工作,我不明白如何,除非该路径是一个符号链接,这似乎是错误的。
使用Yosemite,您可以通过属性列表或plist添加环境变量。 例如,〜/ Library / LaunchAgents / environment.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>DGG.Environment</string>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>-c</string>
<string>
launchctl setenv JAVA_HOME $(/usr/libexec/java_home -v 1.8)
launchctl setenv M2_HOME /Users/dgg/JavaTools/apache-maven-3.3.9
launchctl setenv M2 /Users/dgg/JavaTools/apache-maven-3.3.9/bin
launchctl setenv MAVEN_OPTS "-Xms512m -Xmx1024m"
</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
更改plist文件后,从shell运行此命令以加载更改:
launchctl load ~/Library/LaunchAgents/environment.plist
请注意, <string>
标记中的多个值都可以正确执行,但我无法使用此方法来修改$ PATH变量,所以我只是在我的.bash_profile中添加$ JAVA_HOME / bin和$ M2到路径。 我确信还有其他方法可以做到,但这很有效。
希望有所帮助,
DGG
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.