0

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

enter image description here

What went wrong and how to fix it?

Bitwise DEVS
  • 2,858
  • 4
  • 24
  • 67
  • You can't do `ln -s PATH WITH SPACES ...` You must do `ln -s "PATH WITH SPACES" ...` – Mark Setchell Aug 08 '23 at 19:07
  • @MarkSetchell thanks I will try that, also can I delete the broken symlinks using Finder app itself? – Bitwise DEVS Aug 08 '23 at 19:08
  • @MarkSetchell I'm pretty sure you should *not* do that -- the `npm` and `npx` symlinks are part of `node`, and should not be deleted. – Gordon Davisson Aug 08 '23 at 19:28
  • @BitwiseDEVS There's another problem here, and it means you can't use the symlink method. The symlink method tries to *replace* the normal /usr/local/bin directory with a symlink to the Android Studios bin directory, but you already have a real /usr/local/bin directory with `node` and *its* symlinks there; you'd have to remove the entire directory (thus breaking `node`) in order to replace it with a symlink to the Android Studios bin directory. So... pick a different method to add that directory to `PATH`. – Gordon Davisson Aug 08 '23 at 19:30
  • @GordonDavisson Ok, thank you. I'll delete my suggestion right now. – Mark Setchell Aug 08 '23 at 19:30
  • @GordonDavisson I excluded Mark's suggestion of removing npm, npx, and those that I did not add manually only deleting those that I added. However I discovered that even running the keytool command in the actual bin directory it still failing with error `The operation couldn’t be completed. Unable to locate a Java Runtime. `. I am not sure why it is not working since in Windows it works properly and I am expecting that Android Studio's JDK is already being use thus no need for me to install it. – Bitwise DEVS Aug 08 '23 at 19:35
  • @GordonDavisson does it mean doing that symlink in this case will not work at all? Thanks – Bitwise DEVS Aug 08 '23 at 20:05
  • @BitwiseDEVS It means that in order to make the symlink work, you'd need to delete part of your `node` setup. Essentially, with the symlink method, you can have Android Studios *or* `node`, but not both. – Gordon Davisson Aug 08 '23 at 22:31
  • @GordonDavisson I can only symlink one which is node in this case? – Bitwise DEVS Aug 08 '23 at 22:32
  • 1
    @BitwiseDEVS node isn't using a symlink, it's using an actual directory with files in it. This is the standard way to do things, and can coexist with lots of other things (as long as they *also* use the actual-directory-with-files system). The symlink method, on the other hand, is a nonstandard kluge which cannot coexist with anything else that needs /usr/local/bin. – Gordon Davisson Aug 08 '23 at 23:44

1 Answers1

0

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.

Bitwise DEVS
  • 2,858
  • 4
  • 24
  • 67