Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scoop reset does not switch java version #3822

Closed
goostleek opened this issue Jan 16, 2020 · 21 comments
Closed

scoop reset does not switch java version #3822

goostleek opened this issue Jan 16, 2020 · 21 comments

Comments

@goostleek
Copy link
Sponsor Member

I followed the guide for switching java versions but it somehow seems to not work in my case.

I have both openjdk11 and openjdk13 installed from the java bucket:

$ scoop info openjdk11
Name: openjdk11
Description: Official production-ready open-source builds of OpenJDK 11
Version: 11.0.2-9
Website: https://jdk.java.net/11
License: GPL-2.0-only WITH Classpath-exception-2.0 (https://spdx.org/licenses/GPL-2.0-only WITH Classpath-exception-2.0.html)
Manifest:
  C:\Users\marcin\scoop\buckets\java\bucket\openjdk11.json
Installed:
  C:\Users\marcin\scoop\apps\openjdk11\11.0.2-9
Environment:
  JAVA_HOME=C:\Users\marcin\scoop\apps\openjdk11\current
  PATH=%PATH%;C:\Users\marcin\scoop\apps\openjdk11\11.0.2-9\bin
$ scoop info openjdk13
Name: openjdk13
Description: Official production-ready open-source builds of OpenJDK 13
Version: 13.0.2-8
Website: https://jdk.java.net/13
License: GPL-2.0-only WITH Classpath-exception-2.0 (https://spdx.org/licenses/GPL-2.0-only WITH Classpath-exception-2.0.html)
Manifest:
  C:\Users\marcin\scoop\buckets\java\bucket\openjdk13.json
Installed:
  C:\Users\marcin\scoop\apps\openjdk13\13.0.2-8
Environment:
  JAVA_HOME=C:\Users\marcin\scoop\apps\openjdk11\current
  PATH=%PATH%;C:\Users\marcin\scoop\apps\openjdk13\13.0.2-8\bin

The current java is openjdk11 in my case

$ scoop which java
C:\Users\marcin\scoop\apps\openjdk11\current\bin\java.exe

Now I want to switch to openjdk13 by using scoop reset command (as mentioned in the guide)

$ scoop reset openjdk13
Resetting openjdk13 (13.0.2-8).
Linking ~\scoop\apps\openjdk13\current => ~\scoop\apps\openjdk13\13.0.2-8

The command ends with success. However, the java version is not switched. It is still locked at openjdk11:

$ scoop which java
C:\Users\marcin\scoop\apps\openjdk11\current\bin\java.exe

A quick investigation revealed two java executables on the %PATH%

$ where java

C:\Users\marcin\scoop\apps\openjdk11\current\bin\java.exe
C:\Users\marcin\scoop\apps\openjdk13\current\bin\java.exe

So the shell will always take the first one available, openjdk11 in my case.

Is the the scoop reset command broken ATM? Or, maybe I misunderstood the scoop reset command?

My scoop version is

$ scoop -v
Current Scoop version:
a9fa775d (HEAD -> master, origin/master, origin/HEAD) chore(buckets.json): Update scoop-nonportable URL (#3776)

'extras' bucket:
8c1326b0 (HEAD -> master, origin/master, origin/HEAD) postman: Update to version 7.16.0

'giantswarm' bucket:
47b39b1 (HEAD -> master, origin/master, origin/HEAD) Update gsctl to 0.18.1

'java' bucket:
6a6b346 (HEAD -> master, origin/master, origin/HEAD) adopt8-openj9-nightly: Update to version 202001161141

'jetbrains' bucket:
30fbf9f (HEAD -> master, origin/master, origin/HEAD) WebStorm-EAP: Update to version 2019.3.2-193.6015.20

'main' bucket:
8139a381 (HEAD -> master, origin/master, origin/HEAD) jfrog: Update to version 1.33.1

'nonportable' bucket:
0377c8a (HEAD -> master, origin/master, origin/HEAD) virtualbox52-np: Update to version 5.2.36

'versions' bucket:
87b59aa (HEAD -> master, origin/master, origin/HEAD) rclone-beta: Update to version 1.50.2-139-g1ba5e991
@niheaven
Copy link
Member

Does restarting console solve this problem?

@goostleek
Copy link
Sponsor Member Author

No, it does not. I forgot to mention it.

@lfire
Copy link

lfire commented Feb 19, 2020

me too,
I find the problem only preduce in the situation of "install with global".

@Smail
Copy link

Smail commented May 15, 2020

I think the problem is, that openjdk11 and openjdk13 are different packages and therefore you can't switch the version with openjdk@<version> as openjdk in this context is a completely different package to scoop. You'd have to install openjdk, but the problem with this approach is, that the openjdk package - or rather its scoop manifest file - only contains the most recent version, which is currently 14. This is a bug in my opinion and should be addressed.

My current workaround is to execute
[Environment]::setEnvironmentVariable("JAVA_HOME", "$home\scoop\apps\<JDK>\current\", "User") (PowerShell) when you want to change the java version. Of course you need to change the part saying <JDK> to whatever JDK installation you have. In your example this would be openjdk11 or openjdk13. You can use this PowerShell script. Just add it to a directory included in your path variable.

Also, you need to add %JAVA_HOME%bin to the very beginning of your path variable, because every time you install a new java version, it gets added to the very beginning of your path variable and also because just changing the directory to where JAVA_HOME points to doesn't change which java executable gets called, since this is only determined by your path variable's order (the first executable in your path with the specified name is always the one, which gets executed).

Note: If you installed scoop in a different directory, than the default, you'd need to use the SCOOP environment variable instead of $home\scoop (I might be wrong with the existence of the SCOOP variable so correct me if I'm wrong, but then again you might just add it yourself).

@NotWearingPants
Copy link

I believe this is because scoop reset doesn't reorder PATH directories added by "env_add_path", only recreates shims and environment variables (although I haven't checked that this is the case).

This will probably be solved if both openjdk packages will list java.exe under "bin" instead of using "env_add_path".

@goostleek
Copy link
Sponsor Member Author

@Smail Yes, this is exactly the reason you wrote.

I think the problem is, that openjdk11 and openjdk13 are different packages and therefore you can't switch the version with openjdk@<version> as openjdk in this context is a completely different package to scoop.

The documentation about switching Java versions is misleading because it says the opposite as it should work as intended 🤷‍♂️.

@deki
Copy link

deki commented Sep 9, 2020

+1 from my side, just got confused by the documentation

There is a corretto, a corretto8 and a corretto11 package. To make it work with reset we would need both version in one package, right?

@NSP-0123456
Copy link

I use corretto, sapmachine and openjdk and would like to switch from one to another using scoop reset Vendor@version.
the best would be to have the path reordered when using reset to targeted jdk !
Currently I have a generic java folder in the path and use a junction to switch from one to another folder but this less flexible than a per console solution.

@gavenkoa
Copy link

gavenkoa commented Dec 2, 2020

Currently I have a generic java folder in the path and use a junction to switch from one to another folder but this less flexible than a per console solution.

Pretty cool solution. I think it should be integrated into Scoop core.

Define "platforms", like python, ruby, java, Add platform bin into PATH and than switch platform bin into real installation via directory junction (symlink seem require elevation).

The benefit of solution - it is only few IO calls to FS, no need to restart apps to pick up updated PATH, will work from CMD.exe, Cygwin, WSL!

Command for managing platforms might emit helpers for CMD/WSL/Cygwin for other env vars (like JAVA_HOME), examples ready to be inserted into shell:

CMD: set JAVA_HOME=%SCOOP%\apps\openjdk15\current
WSL: export JAVA_HOME=/mnt/c/opt/scoop/apps/openjdk15/current
Cygwin: export JAVA_HOME=/cygdrive/c/opt/scoop/apps/openjdk15/current

@gavenkoa
Copy link

gavenkoa commented Dec 2, 2020

I think that JAVA_HOME / GRAALVM_HOME can point to "Java platform" folder switchable via junction to latest Java too.

@ghost
Copy link

ghost commented Feb 9, 2022

Seems to be fixed now.

@HUMORCE
Copy link
Member

HUMORCE commented Feb 9, 2022

> scoop list zulu
Installed apps matching 'zulu':
  zulu11-jdk 11.54.23 [java]
  zulu17-jdk 17.32.13 [java]

> scoop which java
C:\Users\HUMOR\scoop\apps\zulu11-jdk\current\bin\java.exe

> java --version
openjdk 11.0.14 2022-01-18 LTS
OpenJDK Runtime Environment Zulu11.54+23-CA (build 11.0.14+9-LTS)
OpenJDK 64-Bit Server VM Zulu11.54+23-CA (build 11.0.14+9-LTS, mixed mode)

> scoop reset zulu17-jdk
Resetting zulu17-jdk (17.32.13).
Linking ~\scoop\apps\zulu17-jdk\current => ~\scoop\apps\zulu17-jdk\17.32.13

> scoop which java
C:\Users\HUMOR\scoop\apps\zulu17-jdk\current\bin\java.exe

> java --version
openjdk 17.0.2 2022-01-18 LTS
OpenJDK Runtime Environment Zulu17.32+13-CA (build 17.0.2+8-LTS)
OpenJDK 64-Bit Server VM Zulu17.32+13-CA (build 17.0.2+8-LTS, mixed mode, sharing)

@HUMORCE HUMORCE closed this as completed Feb 9, 2022
@gphg
Copy link

gphg commented May 13, 2022

I have Java 17 (Microsoft) installed on System (installed automatically by VSCode. For reason I need Java RE 8 (Oracle), I get this installed via Scoop. Now I can't switch to System and the current %JAVA_HOME% is pointing to scoop.

At the moment, I have to delete the user's variable %JAVA_HOME% and related binary path on %PATH% to use the System. I wish there is a way to switch between Scoop (user) and System.

@HUMORCE
Copy link
Member

HUMORCE commented May 14, 2022

I have Java 17 (Microsoft) installed on System (installed automatically by VSCode.

Which extension did this?

@gphg
Copy link

gphg commented May 16, 2022

@HUMORCE it was the one on VSCode docs web site. The Java section.

@HUMORCE
Copy link
Member

HUMORCE commented May 17, 2022

To help you set up quickly, we recommend you use the Coding Pack for Java, which is the bundle of VS Code, the Java Development Kit (JDK), and a collection of suggested extensions by Microsoft. The Coding Pack can also be used to fix an existing development environment.

https://code.visualstudio.com/docs/languages/java#_install-visual-studio-code-for-java

A Java Development Kit (JDK) is a software development environment used for developing Java applications. In order to run Java within Visual Studio Code, you need to install a JDK. The Extension Pack for Java supports Java version 1.5 or above.

...

Note: If you have multiple JDKs installed and need to use a specific JDK version for your projects, see Configure Runtime for Projects. To enable Java preview features, see How can I use VS Code with new Java versions.

https://code.visualstudio.com/docs/languages/java#_install-a-java-development-kit-jdk

this is not a mandatory option. for switching java between other installation(s and scoop installation(s, you need to write a script yourself.

@gphg
Copy link

gphg commented May 17, 2022

I see.

@tauinger-de
Copy link

Sorry, but I am confused about the current state of this issue - when I follow the Java Switching example on the Java wiki I don't get the same results as are documented -- it basically doesn't work for me.

I am a scoop beginner and maybe don't understand all aspects but at least I would wish for a documentation that describes a working solution and also a clear statement what JDKs are switchable and which are not - the wiki says "currently this isn't working for all available packages" but doesn't go into more detail.

PS loving scoop so far!! easy java switching would be supercool

@HUMORCE
Copy link
Member

HUMORCE commented Feb 29, 2024

I am a scoop beginner and maybe don't understand all aspects but at least I would wish for a documentation that describes a working solution and also a clear statement what JDKs are switchable and which are not - the wiki says "currently this isn't working for all available packages" but doesn't go into more detail.

scoop-reset switches apps by overriding shims, environment variables, etc. or change PATH order, which means it can siwtch apps installed by Scoop.

Example: 1 JDK are external, and 2 JDKs are Scooped.

c:\jdk18
c:\users\foo\scoop\apps\temurin11-jdk
c:\users\foo\scoop\apps\temurin17-jdk

When ...\scoop\shims has a higher priority in the PATH than c:\jdk18(for jdks, this is does not matter, but others), scoop-reset works.

❯ scoop reset temurin11-jdk
Resetting temurin11-jdk (11.0.22-7).
Linking ~\scoop\apps\temurin11-jdk\current => ~\scoop\apps\temurin11-jdk\11.0.22-7

❯ $env:PATH
C:\Users\humorce\scoop\apps\temurin11-jdk\current\bin;C:\Users\humorce\scoop\apps\temurin17-jdk\current\bin;C:\Program Files\PowerShell\7;...

❯ scoop reset temurin17-jdk
Resetting temurin17-jdk (17.0.10-7).
Linking ~\scoop\apps\temurin17-jdk\current => ~\scoop\apps\temurin17-jdk\17.0.10-7

❯ $env:PATH
C:\Users\humorce\scoop\apps\temurin17-jdk\current\bin;C:\Users\humorce\scoop\apps\temurin11-jdk\current\bin;C:\Program Files\PowerShell\7;...

@tauinger-de
Copy link

Okay, I identified whats going on. Resetting the JDK version does only work within a Powershell (installing a new JDK and auto-switchting to it).

Apologies if this is documented somewhere but maybe its a bug? Anyways if possible a warning should be displayed when trying to switch from a non-powershell terminal.

@HUMORCE
Copy link
Member

HUMORCE commented Mar 1, 2024

For Command Prompt(cmd.exe), it works on future sessions.

Scoop/lib/core.ps1

Lines 945 to 955 in f930280

function add_first_in_path($dir, $global) {
$dir = fullpath $dir
# future sessions
$null, $currpath = strip_path (env 'path' $global) $dir
env 'path' $global "$dir;$currpath"
# this session
$null, $env:PATH = strip_path $env:PATH $dir
$env:PATH = "$dir;$env:PATH"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests