shyiko / jabba

(cross-platform) Java Version Manager
2.97k stars 194 forks source link

`jabba alias default amazon-corretto@1.17.0-0.35.1` doesn't work #831

Open gusfrehse opened 2 years ago

gusfrehse commented 2 years ago

I am on Windows.

Every time I open a shell I need to jabba use amazon-corretto@1.17.0-0.35.1. If not the java executable is not found. jabba current has no output.

I think the jabba alias default amazon-corretto@1.17.0-0.35.1 is supposed to do exactly this, but even after doing the command, new shells can't find java.

This is my .jabba:

Diretório: C:\Users\gusfr\.jabba

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        07/02/2022     20:31                bin
d-----        08/02/2022     11:39                jdk
-a----        08/02/2022     11:47             29 default.alias
-a----        07/02/2022     20:31           1060 jabba.ps1

and .jabba\default.alias is

amazon-corretto@1.17.0-0.35.1
ZaLiTHkA commented 2 years ago

edit: after writing all of this, I just re-re-read the README, and according to...

# set default java version on shell (since 0.2.0)
# this version will automatically be "jabba use"d every time you open up a new terminal
jabba alias default 1.8

...none of what I've described below should actually be necessary. so..... 🤷🏼


I think what you're experiencing here is quite literally "how Jabba is designed".. going by the second FAQ entry:

Q: How do I switch java globally?

A: jabba doesn't have this functionality built-in because the exact way varies greatly between the operation systems and usually involves elevated permissions. But. Here are the snippets that should work:

[...OS-specific snippets...]

after which it goes on to mention using sudo update-alternatives to update system level java versions. but that is not available in this case, simply because Windows does things differently to Unix.

tl;dr: to get what you're looking for, your best bet is to just make your preferred shell call jabba use on load.


disclaimer: on my system I have one "user level" Java installation from the official Oracle download, so automatic updates work out-of-the-box for my system-level JRE. this does not include the JDK though, which I feel should be activated only as needed anyway.

to do this with PowerShell (which is what I use), you could do the following:

  1. run jabba alias default amazon-corretto@1.17.0-0.35.1 as you normally would, to create the Jabba config default.alias file.
  2. run [YOUR_EDITOR] $PROFILE to open your user PS profile file (which opens %USERPROFILE%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 on your system)
  3. add the following snippet anywhere after the Jabba initialisation call:
# auto-load default Jabba alias, if defined..
if (Test-Path "$env:USERPROFILE\.jabba\default.alias") {
    jabba use default
    echo "now using Java: $(jabba current)"
}

then when you open a new terminal, you should see something like this:

PowerShell 7.2.2
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

now using Java: zulu@1.8.282

..and you should be able to verify the active Java version as follows:

❯ where.exe java.exe
C:\Users\agreeff\.jabba\jdk\zulu@1.8.282\bin\java.exe
C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe

❯ where.exe javac.exe
C:\Users\agreeff\.jabba\jdk\zulu@1.8.282\bin\javac.exe

❯ jabba current
zulu@1.8.282

❯ java -version
openjdk version "1.8.0_282"
OpenJDK Runtime Environment (Zulu 8.52.0.23-CA-win64) (build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM (Zulu 8.52.0.23-CA-win64) (build 25.282-b08, mixed mode)

one critical point to eep in mind: this does not negate the need for a system-level Java installation, as this will only tell Jabba to "use" the default aliased version when you load a PowerShell environment (i.e.: a new command prompt)... nothing I've suggested here will be detected or used when a third-party application needs Java, and they (Eclipse, IDEA, NetBeans, etc..) will still need to be configured with the correct SDK path.

with that said, Jabba itself is installed at a system level, so you might be able to leverage jabba which [VERSION] to help with detecting this at launch of said third-party app. I haven't tested this myself though, so YMMV...

...and lastly, I also have not tried to make this auto-load a project level config (such as calling jabba use to read from a local .jabbarc file), but in theory you could extend the PowerShell profile snippet above to do something like: jabba use, then if the output of jabba current is empty, call jabba use default. I would warn against putting too much logic in your PowerShell profile script though, the last thing you want is to wait 3 seconds every time you open a terminal.

anyways.. hope that helps a bit. 🖖🏼

ZaLiTHkA commented 2 years ago

it's so much fun redoing dev environments.

you can't remember how something worked before, so you go searching online.. you find a solution, which you yourself shared.. lol.

...none of what I've described below should actually be necessary. so..... 🤷🏼

yet we still need to.. I have trimmed the "auto-load default" down a tiny bit though, so all relevant $PROFILE lines now read:

# initialise Jabba..
if (Test-Path "$env:JABBA_HOME\jabba.ps1") { . "$env:JABBA_HOME\jabba.ps1" }

# auto-load default Jabba alias, if defined..
if (Test-Path "$env:JABBA_HOME\default.alias") { jabba use default }
vuhieutopicus commented 1 year ago

@ZaLiTHkA Your solution is worked. Thank you