如何通过符号链接将Android Studio JRE或JBR添加到$PATH并使用它?

huangapple go评论89阅读模式
英文:

How to add and use Android Studio JRE or JBR in $PATH via symlink?

问题

从Windows系统转到macOS系统后,你想将Android Studio的JRE(现在称为JBR)目录添加到环境变量路径中,以便能够在命令行中直接运行像jarsigner和keytool这样的可执行文件。你在网上找到了一种建议的方法,即使用符号链接(symlink)来实现。

首先,你将目录复制到剪贴板中:

/Applications/Android Studio Preview.app/Contents/jbr/Contents/Home/bin

然后,按照这个Stack Overflow的回答执行以下命令:

sudo ln -s /Applications/Android Studio Preview.app/Contents/jbr/Contents/Home/bin /usr/local/bin

你已经使用echo $PATH命令验证了usr/local/bin已经存在,所以跳过了创建目录的步骤。不幸的是,这个方法并没有生效,似乎出现了问题。

你看到的结果如下:

user@User-MacBook-Pro ~ % ls -la /usr/local/bin/
total 354896
drwxr-xr-x  9 root  wheel        288 Aug  9 02:31 .
drwxr-xr-x  6 root  wheel        192 Aug  8 01:12 ..
lrwxr-xr-x  1 root  wheel         21 Aug  9 02:31 Android -> /Applications/Android
lrwxr-xr-x  1 root  wheel          6 Aug  9 02:31 Studio -> Studio
lrwxr-xr-x  1 root  wheel         42 Aug  9 02:31 bin -> Preview.app/Contents/jbr/Contents/Home/bin
lrwxr-xr-x  1 root  wheel         45 Aug  8 02:39 corepack -> ../lib/node_modules/corepack/dist/corepack.js
-rwxr-xr-x  1 root  wheel  181706736 Jul 18 20:09 node
lrwxr-xr-x  1 root  wheel         38 Aug  8 02:39 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxr-xr-x  1 root  wheel         38 Aug  8 02:39 npx -> ../lib/node_modules/npm/bin/npx-cli.js

出现了问题,你想知道是什么出错了,以及如何修复。

英文:

Coming from Windows, you can easily add Android Studio's JRE now JBR directory in environment variable path where executable such as jarsigner and keytool are located. I tried to do the same in macOS but using symlink as the suggested method I am seeing in the web.

First I copied the directory to my clipboard

/Applications/Android Studio Preview.app/Contents/jbr/Contents/Home/bin

Then followed this SO answer and executed the below command

sudo ln -s /Applications/Android Studio Preview.app/Contents/jbr/Contents/Home/bin /usr/local/bin

I already echoed $PATH and found usr/local/bin so I skipped the mkdir part.
Unfortunately it does not work and seems broken.

This is what I am seeing

user@User-MacBook-Pro ~ % ls -la /usr/local/bin/
total 354896
drwxr-xr-x  9 root  wheel        288 Aug  9 02:31 .
drwxr-xr-x  6 root  wheel        192 Aug  8 01:12 ..
lrwxr-xr-x  1 root  wheel         21 Aug  9 02:31 Android -> /Applications/Android
lrwxr-xr-x  1 root  wheel          6 Aug  9 02:31 Studio -> Studio
lrwxr-xr-x  1 root  wheel         42 Aug  9 02:31 bin -> Preview.app/Contents/jbr/Contents/Home/bin
lrwxr-xr-x  1 root  wheel         45 Aug  8 02:39 corepack -> ../lib/node_modules/corepack/dist/corepack.js
-rwxr-xr-x  1 root  wheel  181706736 Jul 18 20:09 node
lrwxr-xr-x  1 root  wheel         38 Aug  8 02:39 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxr-xr-x  1 root  wheel         38 Aug  8 02:39 npx -> ../lib/node_modules/npm/bin/npx-cli.js

如何通过符号链接将Android Studio JRE或JBR添加到$PATH并使用它?

What went wrong and how to fix it?

答案1

得分: 0

在搜索了几个小时后,我终于从这个SO 答案中找到了我要找的内容。

首先检查 java_home 是否在 /usr/libexec 中。

然后检查 /Library/Java/JavaVirtualMachines 目录是否存在。

确认后,在我的情况下获取或复制 Android Studio JBR 的路径,即 /Applications/Android Studio Preview.app/Contents/jbr

注意到路径中有空格,你可以添加转义字符或者将整个路径用双引号括起来。

注意双引号 ",如果从启用了智能引号替换的应用程序行 Notes 中复制,它会将常规引号更改为“左双引号。

然后你可以使用以下命令创建符号链接,而不是将其复制粘贴到 JavaVirtualMachines 文件夹中。

sudo ln -s "/Applications/Android Studio Preview.app/Contents/jbr" /Library/Java/JavaVirtualMachines

成功后执行以下命令:

export JAVA_HOME=$(/usr/libexec/java_home)

现在你应该能够运行那些在 JRE 或 JBR bin 中可用的命令,比如 keytoolsjarsigner,同时设置和修复你的 JAVA_HOME 设置。

英文:

After searching for hours I finally got what I am looking for from this SO answer

First check if java_home is in /usr/libexec

Then check if /Library/Java/JavaVirtualMachines directory exist

After confirming, get or copy the Android Studio JBR path in my case this is /Applications/Android Studio Preview.app/Contents/jbr

Noticed that there is spaces in path, what you can do is either add escape or just wrap the whole path with double quote.

Be careful with the double quote " as copying from apps line Notes that has Smart Quote Substitution enabled, it will change your regular quotes to “ Left Double Quotation.

Then you will have this command to create symlink instead of copy pasting it to JavaVirtualMachines folder.

sudo ln -s "/Applications/Android Studio Preview.app/Contents/jbr" /Library/Java/JavaVirtualMachines

Once success execute this command

export JAVA_HOME=$(/usr/libexec/java_home)

You should now be able to run those commands that are available inside JRE or JBR bin such as keytools and jarsigner while also setting and fixing your JAVA_HOME setup.

huangapple
  • 本文由 发表于 2023年8月9日 03:02:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定