简体   繁体   English

如何更改从 /usr/libexec/java_home 返回的 Mac OS 的默认 Java VM

[英]How can I change Mac OS's default Java VM returned from /usr/libexec/java_home

(Wasn't sure if this should go on SU... migration is certainly an option, but more programmers read questions here, so here goes). (不确定这是否应该在 SU 上使用 go ......迁移当然是一种选择,但更多的程序员在这里阅读问题,所以这里是)。

I am running Mac OS X 10.8.4, and I have Apple's JDK 1.6.0_51 installed as well as Oracle's JDK 1.7.0_25.我正在运行 Mac OS X 10.8.4,并且安装了 Apple 的 JDK 1.6.0_51 以及 Oracle 的 JDK 1.7.0_25。 I recently installed Oracle's 1.8 preview JDK for some pre-release software that requires it.我最近为一些需要它的预发布软件安装了 Oracle 的 1.8 预览版 JDK。 Now, when I run /usr/libexec/java_home, I get this:现在,当我运行 /usr/libexec/java_home 时,我得到了这个:

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
    1.8.0, x86_64:  "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
    1.7.0_25, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home
    1.6.0_51-b11-457, x86_64:   "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    1.6.0_51-b11-457, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

Great.伟大的。

However, running:但是,运行:

$ java -version

Returns:回报:

java version "1.8.0-ea"

That means that the default version of Java is currently the pre-release version, which breaks some "normal" packages (in my case, VisualVM).这意味着 Java 的默认版本当前是预发布版本,它破坏了一些“正常”包(在我的例子中是 VisualVM)。

I can't set JAVA_HOME because launching applications ignores environment variables, even when launching from the command line (eg $ open /Applications/VisualVM.app ).我无法设置JAVA_HOME ,因为启动应用程序会忽略环境变量,即使从命令行启动(例如$ open /Applications/VisualVM.app )也是如此。

So, is there a file I can edit where I can set my JVM ordering preferences globally ?那么,有没有我可以编辑的文件,我可以在其中设置我的 JVM全局订购首选项?

(Please don't tell me to launch the Java Preferences Panel because that simply does not work: it does not contain anything useful and only lists one of the 4 JVMs that I have installed.) (请不要告诉我启动 Java 首选项面板,因为这根本不起作用:它不包含任何有用的东西,只列出了我安装的 4 个 JVM 之一。)

Update :更新

Oracle JVMs live in /Library/Java/JavaVirtualMachines . Oracle JVM 位于/Library/Java/JavaVirtualMachines中。 Re-naming the JDK 1.8 directory to jdk1.8.0.jvm.xyz does not change anything: java_home still finds it in the right place, and running /usr/bin/java still executes the 1.8 JVM.将 JDK 1.8 目录重命名为jdk1.8.0.jvm.xyz并没有改变任何东西: java_home仍然在正确的位置找到它,运行 /usr/bin/java 仍然执行 1.8 JVM。 This is not an issue with synlinks, etc.这不是synlinks等的问题。

Answers to Similar Questions类似问题的答案

While this answer offers what amounts to a hack that will remove versions of Java from being picked up by java_home, it still does not answer this question of how java_home chooses its default and whether or not users can non-destructively set it .虽然这个答案提供了相当于一个 hack,将删除 Java 的版本,使其不被 java_home 拾取,但它仍然没有回答 java_home如何选择其默认值以及用户是否可以非破坏性设置它的问题

I think JAVA_HOME is the best you can do.我认为JAVA_HOME是你能做的最好的。 The command-line tools like java and javac will respect that environment variable, you can use /usr/libexec/java_home -v '1.7*' to give you a suitable value to put into JAVA_HOME in order to make command line tools use Java 7.javajavac这样的命令行工具会尊重那个环境变量,你可以使用/usr/libexec/java_home -v '1.7*'给你一个合适的值来放入JAVA_HOME以便让命令行工具使用 Java 7 .

export JAVA_HOME="`/usr/libexec/java_home -v '1.7*'`"

But standard double-clickable application bundles don't use JDKs installed under /Library/Java at all.但是标准的可双击应用程序包根本不使用安装在/Library/Java下的 JDK。 Old-style .app bundles using Apple's JavaApplicationStub will use Apple Java 6 from /System/Library/Frameworks , and new-style ones built with AppBundler without a bundled JRE will use the "public" JRE in /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home - that's hard-coded in the stub code and can't be changed, and you can't have two different public JREs installed at the same time.使用 Apple 的JavaApplicationStub的旧式.app包将使用来自/System/Library/Frameworks Apple Java 6,而使用AppBundler 构建的没有捆绑 JRE 的新式包将使用/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home的“公共”JRE /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home - 在存根代码中进行了硬编码并且无法更改,并且您不能同时安装两个不同的公共 JRE。


Edit: I've had a look at VisualVM specifically, assuming you're using the "application bundle" version from the download page , and this particular app is not an AppBundler application, instead its main executable is a shell script that calls a number of other shell scripts and reads various configuration files.编辑:我已经专门查看了 VisualVM,假设您使用的是下载页面中的“应用程序包”版本,并且这个特定的应用程序不是 AppBundler 应用程序,而是它的主要可执行文件是一个调用数字的 shell 脚本其他 shell 脚本并读取各种配置文件。 It defaults to picking the newest JDK from /Library/Java as long as that is 7u10 or later, or uses Java 6 if your Java 7 installation is update 9 or earlier.它默认从/Library/Java选择最新的 JDK,只要它是 7u10 或更高版本,或者如果您的 Java 7 安装是更新 9 或更早版本,则使用 Java 6。 But unravelling the logic in the shell scripts it looks to me like you can specify a particular JDK using a configuration file.但是解开 shell 脚本中的逻辑,在我看来,您可以使用配置文件指定特定的 JDK。

Create a text file ~/Library/Application Support/VisualVM/1.3.6/etc/visualvm.conf (replace 1.3.6 with whatever version of VisualVM you're using) containing the line创建包含行的文本文件~/Library/Application Support/VisualVM/1.3.6/etc/visualvm.conf (用您使用的任何版本的 VisualVM 替换 1.3.6)

visualvm_jdkhome="`/usr/libexec/java_home -v '1.7*'`"

and this will force it to choose Java 7 instead of 8.这将迫使它选择 Java 7 而不是 8。

I've been there too and searched everywhere how /usr/libexec/java_home works but I couldn't find any information on how it determines the available Java Virtual Machines it lists.我也去过那里,到处搜索/usr/libexec/java_home是如何工作的,但我找不到任何关于它如何确定它列出的可用 Java 虚拟机的信息。

I've experimented a bit and I think it simply executes a ls /Library/Java/JavaVirtualMachines and then inspects the ./<version>/Contents/Info.plist of all runtimes it finds there.我进行了一些实验,我认为它只是执行ls /Library/Java/JavaVirtualMachines然后检查它在那里找到的所有运行时的./<version>/Contents/Info.plist

It then sorts them descending by the key JVMVersion contained in the Info.plist and by default it uses the first entry as its default JVM.然后它按照 Info.plist 中包含的键JVMVersion对它们进行降序排序,默认情况下它使用第一个条目作为它的默认 JVM。

I think the only thing we might do is to change the plist: sudo vi /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Info.plist and then modify the JVMVersion from 1.8.0 to something else that makes it sort it to the bottom instead of the top, like !1.8.0 .我认为我们唯一可能做的就是更改 plist: sudo vi /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Info.plist然后将 JVMVersion 从1.8.0修改为其他内容,使其排序它到底部而不是顶部,如!1.8.0

Something like:就像是:

<?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>
    ...
    <dict>
            ...
            <key>JVMVersion</key>
            <string>!1.8.0</string>   <!-- changed from '1.8.0' to '!1.8.0' -->`

and then it magically disappears from the top of the list:然后它神奇地从列表的顶部消失了:

/usr/libexec/java_home -verbose
Matching Java Virtual Machines (3):
    1.7.0_45, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
    1.7.0_09, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home
    !1.8.0, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home

Now you will need to logout/login and then:现在您需要注销/登录,然后:

java -version
java version "1.7.0_45"

:-) :-)

Of course I have no idea if something else breaks now or if the 1.8.0-ea version of java still works correctly.当然,我不知道现在是否还有其他问题,或者 1.8.0-ea 版本的 java 是否仍然可以正常工作。

You probably should not do any of this but instead simply deinstall 1.8.0.您可能不应该这样做,而应该简单地卸载 1.8.0。

However so far this has worked for me.但是到目前为止,这对我有用。

It's actually pretty easy.这实际上很容易。 Let's say we have this in our JavaVirtualMachines folder:假设我们的 JavaVirtualMachines 文件夹中有这个:

  • jdk1.7.0_51.jdk jdk1.7.0_51.jdk
  • jdk1.8.0.jdk jdk1.8.0.jdk

Imagine that 1.8 is our default, then we just add a new folder (for example 'old') and move the default jdk folder to that new folder.假设 1.8 是我们的默认值,那么我们只需添加一个新文件夹(例如 'old')并将默认 jdk 文件夹移动到该新文件夹中。 Do java -version again et voila, 1.7!再次执行java -version等等,1.7!

It's pretty simple, if you don't mind rolling up your sleeves... /Library/Java/Home is the default for JAVA_HOME, and it's just a link that points to one of:这很简单,如果你不介意卷起袖子... /Library/Java/Home是 JAVA_HOME 的默认值,它只是一个指向以下之一的链接:

  • /System/Library/Java/JavaVirtualMachines/1.?.?.jdk/Contents/Home /系统/库/Java/JavaVirtualMachines/1.?.?.jdk/Contents/Home
  • /Library/Java/JavaVirtualMachines/jdk1.?.?_??.jdk/Contents/Home /Library/Java/JavaVirtualMachines/jdk1.?.?_??.jdk/Contents/Home

So I wanted to change my default JVM/JDK version without changing the contents of JAVA_HOME... /Library/Java/Home is the standard location for the current JVM/JDK and that's what I wanted to preserve... it seems to me to be the easiest way to change things with the least side effects.所以我想改变我的默认 JVM/JDK 版本而不改变 JAVA_HOME 的内容...... /Library/Java/Home 是当前 JVM/JDK 的标准位置,这就是我想要保留的......在我看来是改变事物的最简单方法,副作用最小。

It's actually really simple.其实很简单。 In order to change which version of java you see with java -version, all you have to do is some version of this:为了使用 java -version 更改您看到的 java 版本,您所要做的就是以下版本:

cd /Library/Java
sudo rm Home
sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home ./Home

I haven't taken the time but a very simple shell script that makes use of /usr/libexec/java_home and ln to repoint the above symlink should be stupid easy to create...我没有花时间但是一个非常简单的 shell 脚本,它使用 /usr/libexec/java_home 和 ln 来重新指向上面的符号链接应该很容易创建......

Once you've changed where /Library/Java/Home is pointed... you get the correct result:一旦你改变了 /Library/Java/Home 的指向......你会得到正确的结果:

cerebro:~ magneto$ java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27) Java HotSpot(TM)
64-Bit Server VM (build 25.60-b23, mixed mode)

I tested "jenv" and other things like setting "JAVA_HOME" without success.我测试了“jenv”和其他诸如设置“JAVA_HOME”之类的东西,但没有成功。 Now I ended up with following solution:现在我最终得到了以下解决方案:

function setJava {
    export JAVA_HOME="$(/usr/libexec/java_home -v $1)"
    launchctl setenv JAVA_HOME $JAVA_HOME
    sudo ln -nsf "$(dirname ${JAVA_HOME})/MacOS" /Library/Java/MacOS 
    java -version
}

(added to ~/.bashrc or ~/.bash.profile or ~/.zshrc) (添加到 ~/.bashrc 或 ~/.bash.profile 或 ~/.zshrc)

And calling like that:并像这样调用:

setJava 1.8

java_home will handle the wrong input. java_home 将处理错误的输入。 So you can't do something wrong.所以你不能做错事。 Maven and other stuff will pick up the right version now. Maven 和其他东西现在会选择正确的版本。

Oracle's uninstallation instructions for Java 7 worked for me. Oracle 的Java 7 卸载说明对我有用。

Excerpt:摘抄:

Uninstalling the JDK To uninstall the JDK, you must have Administrator privileges and execute the remove command either as root or by using the sudo(8) tool.卸载JDK 要卸载JDK,您必须具有管理员权限并以root 身份或使用sudo(8) 工具执行remove 命令。

Navigate to /Library/Java/JavaVirtualMachines and remove the directory whose name matches the following format:*导航到 /Library/Java/JavaVirtualMachines 并删除名称与以下格式匹配的目录:*

/Library/Java/JavaVirtualMachines/jdk<major>.<minor>.<macro[_update]>.jdk

For example, to uninstall 7u6:例如,要卸载 7u6:

% rm -rf jdk1.7.0_06.jdk

A bit late but as this is an ongoing issue with Mac OSX...有点晚了,但因为这是 Mac OSX 的一个持续问题......

The simplest solution I found was to simply remove the OpenJDK stuff that Apple installs.我找到的最简单的解决方案是简单地删除 Apple 安装的 OpenJDK 内容。 Every time an update of Mac OSX arrives it gets installed and you'll need to remove it again.每次 Mac OSX 更新到达时,它都会被安装,您需要再次将其删除。

This works really well if you develop apps for Google App Engine on your mac using Java.如果您在 Mac 上使用 Java 为 Google App Engine 开发应用程序,这会非常有效。 The OpenJDK does not work well and the Java version that comes with the Mac OSX Yosemite upgrade will make the Eclipse Plug-in for App Engine crash on every deployment with the helpful error: "Read timed out". OpenJDK 无法正常工作,Mac OSX Yosemite 升级附带的 Java 版本将使 Eclipse 插件在每次部署时崩溃,并显示有用的错误:“读取超时”。

I actually looked at this a little in the disassembler, since source isn't available.我实际上在反汇编器中看了一点,因为源不可用。

/usr/bin/java and /usr/libexec/java_home both make use of JavaLaunching.framework. /usr/bin/java 和 /usr/libexec/java_home 都使用 JavaLaunching.framework。 The JAVA_HOME environment variable is indeed checked first by /usr/bin/java and friends (but not /usr/libexec/java_home.) The framework uses the JAVA_VERSION and JAVA_ARCH envirionment variables to filter the available JVMs. JAVA_HOME 环境变量确实首先由 /usr/bin/java 和朋友(但不是 /usr/libexec/java_home)检查。该框架使用 JAVA_VERSION 和 JAVA_ARCH 环境变量来过滤可用的 JVM。 So, by default:所以,默认情况下:

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    11.0.5, x86_64: "Amazon Corretto 11"    /Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home
    1.8.0_232, x86_64:  "Amazon Corretto 8" /Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home

But setting, say, JAVA_VERSION can override the default:但是设置,比如说,JAVA_VERSION 可以覆盖默认值:

$ JAVA_VERSION=1.8 /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home

You can also set JAVA_LAUNCHER_VERBOSE=1 to see some additional debug logging as far as search paths, found JVMs, etc., with both /usr/bin/java and /usr/libexec/java_home.您还可以设置 JAVA_LAUNCHER_VERBOSE=1 以查看一些额外的调试日志,如搜索路径、找到的 JVM 等,包括 /usr/bin/java 和 /usr/libexec/java_home。

In the past, JavaLaunching.framework actually used the preferences system (under the com.apple.java.JavaPreferences domain) to set the preferred JVM order, allowing the default JVM to be set with PlistBuddy - but as best as I can tell, that code has been removed in recent versions of macOS.过去,JavaLaunching.framework 实际上使用偏好系统(在 com.apple.java.JavaPreferences 域下)来设置首选 JVM 顺序,允许使用 PlistBuddy 设置默认 JVM - 但据我所知,代码已在最新版本的 macOS 中删除。 Environment variables seem to be the only way (aside from editing the Info.plist in the JDK bundles themselves.)环境变量似乎是唯一的方法(除了在 JDK 包中编辑 Info.plist 本身。)

Setting default environment variables can of course be done through your .profile or via launchd , if you need them be set at a session level.设置默认环境变量当然可以通过您的 .profile 或通过launchd 完成,如果您需要在会话级别设置它们。

It's possible to add to java_home very easily.可以很容易地添加到 java_home。

The way the JAVA_HOME is set on MacOS is as follows. MacOS上JAVA_HOME的设置方式如下。 Add the following to your ~/.zshrc for example.例如,将以下内容添加到您的 ~/.zshrc 中。 The script will pick up the latest JDK installed on your system.该脚本将选择您系统上安装的最新 JDK。

 JAVA_HOME=$(/usr/libexec/java_home)

When the java_home script is run with the -V switch is lists the currently installed JDKs.当使用 -V 开关运行 java_home 脚本时,会列出当前安装的 JDK。 My MacOS (Monterey) default Java Home (JAVA_HOME) was this.我的 MacOS (Monterey) 默认 Java Home (JAVA_HOME) 是这个。

/usr/libexec/java_home -V (Command to list installed JDK versions) 
        1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

Download the version of Java OpenJDK from here .这里下载 Java OpenJDK 的版本。 Make sure to download the JDK .dmg package .确保下载 JDK .dmg package This has an installer that updates the JAVA_HOME .这有一个更新 JAVA_HOME 的安装程序 After installing v18.0 mine changed to the following.安装 v18.0 后,我的更改为以下。

/usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    18 (x86_64) "Azul Systems, Inc." - "Zulu 18.28.13" /Library/Java/JavaVirtualMachines/zulu-18.jdk/Contents/Home
    1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

Now it picks up the top item ie version 18. If the default top item is not preferable, then rejig the names so that the one you want is the first listed as described by void256 above.现在它会选择最上面的项目,即版本 18。如果默认的最上面的项目不是优选的,则重新调整名称,以便您想要的名称是第一个列出的,如上面 void256 所述。 The sort key is here.排序键在这里。

/Library/Java/JavaVirtualMachines/*/Contents/Info.plist.

Now all tools can use the jdk from JAVA_HOME.现在所有工具都可以使用来自 JAVA_HOME 的 jdk。

Simplify the need:-)简化需求:-)

Steps to changes --改变的步骤——

Find the installed version on your machine by executing following command通过执行以下命令在您的机器上找到已安装的版本

/usr/libexec/java_home -V

then you might see like below if there are multiple version of JDK installed如果安装了多个版本的 JDK,您可能会看到如下所示

11.0.16 (arm64) "Homebrew" - "OpenJDK 11.0.16" /opt/homebrew/Cellar/blabla
1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/bala

Create Env file based your machine, for mac like below create or edit the your system Environment file either.zshenv, .zshprofile etc...根据您的机器创建环境文件,对于 mac,如下所示,创建或编辑您的系统环境文件。zshenv、.zshprofile 等...

nano ~/.zshenv
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_292)

Then the magic is done, where ever this symlink used all those java refer to version which you set.然后魔术就完成了,这个符号链接使用了所有 java 引用您设置的版本。 Happy coding Boys & Girls.快乐编码男孩和女孩。

Edit: this information is for visualvm specifically, not for any other java app编辑:此信息专门针对visualvm,不适用于任何其他java应用程序

As mentioned by others, you need to modify the visualvm.conf正如其他人提到的,您需要修改visualvm.conf

For the latest version of JvisualVM 1.3.6 on Mac, the install directories have changed.对于 Mac 上最新版本的 JvisualVM 1.3.6,安装目录已更改。

It is currently in /Applications/VisualVM.app/Contents/Resources/visualvm/etc/visualvm.conf .它目前位于/Applications/VisualVM.app/Contents/Resources/visualvm/etc/visualvm.conf

However this may depend on where you have installed VisualVM.但是,这可能取决于您安装 VisualVM 的位置。 The easiest way to find where your VisualVM is to start it, and then look at the process using:找到 VisualVM 的最简单方法是启动它,然后使用以下命令查看过程:

ps -ef | ps -ef | grep VisualVM可视化虚拟机

You will see something like:你会看到类似的东西:

... -Dnetbeans.dirs=/Applications/VisualVM.app/Contents/Resources/visualvm/visualvm ... ... -Dnetbeans.dirs=/Applications/VisualVM.app/Contents/Resources/visualvm/visualvm ...

You want to take the netbeans.dir property and look up a directory and you will find the etc folder.您想要获取 netbeans.dir 属性并查找目录,您将找到 etc 文件夹。

Uncomment this line in the visualvm.conf and change the path to the jdk在visualvm.conf中取消注释这一行并更改jdk的路径

visualvm_jdkhome="/path/to/jdk"

Additionally, if you are having slowness with your visualvm and you have a lot of memory, I would suggest greatly increasing the amount of memory available and running it in server mode:此外,如果您的 visualvm 运行缓慢并且您有大量内存,我建议大大增加可用内存量并在服务器模式下运行它:

visualvm_default_options="-J-XX:MaxPermSize=96m -J-Xmx2048m -J-Xms2048m -J-server -J-XX:+UseCompressedOops -J-XX:+UseConcMarkSweepGC -J-XX:+UseParNewGC -J-XX:NewRatio=2 -J-Dnetbeans.accept_license_class=com.sun.tools.visualvm.modules.startup.AcceptLicense -J-Dsun.jvmstat.perdata.syncWaitMs=10000 -J-Dsun.java2d.noddraw=true -J-Dsun.java2d.d3d=false"

I had a similar situation, and the following process worked for me:我遇到了类似的情况,以下过程对我有用:

  1. In the terminal, type在终端中,输入

    vi ~/.profile
  2. Then add this line in the file, and save然后在文件中添加这一行,并保存

    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk<version>.jdk/Contents/Home

    where version is the one on your computer, such as 1.7.0_25其中 version 是您计算机上的版本,例如 1.7.0_25

  3. Exit the editor, then type the following command make it become effective退出编辑器,然后输入以下命令使其生效

    source ~/.profile

Then type java -version to check the result然后输入 java -version 查看结果

    java -version 

What is .profile?什么是.profile? From: http://computers.tutsplus.com/tutorials/speed-up-your-terminal-workflow-with-command-aliases-and-profile--mac-30515来自: http : //computers.tutsplus.com/tutorials/speed-up-your-terminal-workflow-with-command-aliases-and-profile--mac-30515

.profile file is a hidden file. .profile 文件是一个隐藏文件。 It is an optional file which tells the system which commands to run when the user whose profile file it is logs in. For example, if my username is bruno and there is a .profile file in /Users/bruno/, all of its contents will be executed during the log-in procedure.它是一个可选文件,它告诉系统在其配置文件所在的用户登录时要运行哪些命令。例如,如果我的用户名是 bruno,并且 /Users/bruno/ 中有一个 .profile 文件,则其所有内容将在登录过程中执行。

MacOS uses /usr/libexec/java_home to find the current Java Version. MacOS 使用 /usr/libexec/java_home 来查找当前的 Java 版本。 One way to bypass is to change the plist file as explained by @void256 above.一种绕过方法是更改​​ plist 文件,如上面@void256 所述。 Other ways is to take the backup of the java_home and replace it with your own script java_home having the code其他方法是备份 java_home 并将其替换为您自己的具有代码的脚本 java_home
echo $JAVA_HOME回声 $JAVA_HOME

Now export the JAVA_HOME to the desired version of the SDK by adding the following commands to the ~/.bash_profile.现在通过将以下命令添加到 ~/.bash_profile 中,将 JAVA_HOME 导出到所需的 SDK 版本。 export JAVA_HOME="/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home" launchctl setenv JAVA_HOME $JAVA_HOME /// Make the environment variable global export JAVA_HOME="/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home" launchctl setenv JAVA_HOME $JAVA_HOME /// 将环境变量设为全局

Run the command source ~/.bash_profile to the run the above commands.运行命令 source ~/.bash_profile 来运行上述命令。

Anytime one needs to change the JAVA_HOME he can reset the JAVA_HOME value in the ~/.bash_profile file.任何时候需要更改 JAVA_HOME,他都可以重置 ~/.bash_profile 文件中的 JAVA_HOME 值。

I wanted to change default java version form 1.6* to 1.7*.我想将默认的 Java 版本从 1.6* 更改为 1.7*。 I tried the following steps and it worked for me:我尝试了以下步骤,它对我有用:

  • Removed link "java" from under /usr/bin从 /usr/bin 下删除链接“java”
  • Created it again, pointing to the new location:再次创建,指向新位置:

ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/java java ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/java java

  • verified with "java -version"用“java -version”验证

java version "1.7.0_51" java版本“1.7.0_51”
Java(TM) SE Runtime Environment (build 1.7.0_51-b13) Java(TM) SE 运行时环境(构建 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode) Java HotSpot(TM) 64 位服务器 VM(构建 24.51-b03,混合模式)

暂无
暂无

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

相关问题 如何将 /usr/libexec/java_home 指向自定义 jdk 安装? - How to point /usr/libexec/java_home to custom jdk installation? /usr/libexec/java_home 返回错误时,如何在 MacOS Monterey 中设置 JAVA_HOME? - How to set JAVA_HOME in MacOS Monterey when /usr/libexec/java_home returns an error? 为什么命令 /usr/libexec/java_home 不起作用? - Why command /usr/libexec/java_home doesn't work? 运行 /usr/libexec/java_home 给出无效目录? - Running /usr/libexec/java_home gives invalid directory? 我该如何解决错误:JAVA_HOME未正确定义。 我们不能执行/ usr / libexec / java_home / bin / java吗? - How do I fix Error: JAVA_HOME is not defined correctly. We cannot execute /usr/libexec/java_home/bin/java? 使 export JAVA_HOME=`/usr/libexec/java_home -v 1.8` 永久化 - Make export JAVA_HOME=`/usr/libexec/java_home -v 1.8` permanent 在 MacOS 升级到 Big Sur 后,使用 /usr/libexec/java_home 切换 java 家庭停止工作 - Switch java home using /usr/libexec/java_home stop working after MacOS upgrade to Big Sur Mac OS如何确定系统路径中存在java_home变量 - mac os how can i be sure that java_home variable is there in the system path mvn -version错误:我们无法执行/ usr / libexec / java_home / bin / java - mvn -version error: We cannot execute /usr/libexec/java_home/bin/java PySpark:“/usr/libexec/java_home/bin/java:不是目录”(macOS Big Sur) - PySpark: "/usr/libexec/java_home/bin/java: Not a directory" (macOS Big Sur)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM