How to Install and Switch Multiple Java Versions on Windows Using Scoop

java dev.to

Do not manually edit environment variables. Learn how to install Scoop and manage multiple Java Development Kit (JDK) versions (Java 17, 21). Learn how to configure Maven and Gradle. Learn how to integrate these tools with IntelliJ and VS Code.

In the past, Java installation on Windows required .exe installers, wizards, and manual changes to environment variables. Now, you can use Scoop. Scoop is a command-line installer for Windows. You can use Scoop to install, manage, and switch between multiple JDKs and build tools. You use a single command.

This guide tells you how to install Scoop, manage Java versions, and configure your development tools.


Step 1: Install Scoop

If you do not have Scoop, you can install it with PowerShell.

  1. Open PowerShell. Make sure that the PowerShell window does not have Administrator privileges.

  2. To let PowerShell run scripts, run this command:

Set-ExecutionPolicy-ExecutionPolicyRemoteSigned-ScopeCurrentUser
Enter fullscreen mode Exit fullscreen mode
  1. To install Scoop, run this command:
irmget.scoop.sh|iex
Enter fullscreen mode Exit fullscreen mode
  1. To make sure that the installation is complete, run this command:
scoop--version
Enter fullscreen mode Exit fullscreen mode

Step 2: Add the Java bucket

By default, Scoop installs standard desktop applications. To install Java distributions, you must add the official Java bucket. A bucket is a managed repository of software manifests.

  1. Run this command in your terminal:
scoopbucketaddjava
Enter fullscreen mode Exit fullscreen mode

Scoop can now access many JDKs. These JDKs include OpenJDK, Oracle JDK, Eclipse Temurin, Amazon Corretto, Zulu, and Microsoft JDK.


Step 3: Search for and install Java versions

You can search for available Java packages with the scoop search jdk command. Install two different versions to learn how to switch between them.

  1. To install the latest OpenJDK, run this command:
scoopinstallopenjdk
Enter fullscreen mode Exit fullscreen mode
  1. To install specific Long-Term Support (LTS) versions, run these commands:
scoopinstallopenjdk17scoopinstalltemurin21-lts
Enter fullscreen mode Exit fullscreen mode

Step 4: Switch between Java versions

Scoop uses symlinks (shortcuts). You do not manually change path variables. When you switch versions, Scoop updates the shortcut.

  1. To view your installed Java versions, run this command:
scooplist
Enter fullscreen mode Exit fullscreen mode

Find your installed Java packages in the output list.

  1. To make a version active, use the scoop reset command and the package name. For example:
scoopresetopenjdk17
Enter fullscreen mode Exit fullscreen mode
  1. To make sure that the version is active, run this command:
java-version
Enter fullscreen mode Exit fullscreen mode

If you must switch back to Java 21, run scoop reset temurin21-lts.


Step 5: Install Maven and Gradle

Scoop lets you install ecosystem tools easily. These tools are available in the default Scoop bucket.

  1. To install Maven, run this command:
scoopinstallmaven
Enter fullscreen mode Exit fullscreen mode
  1. To install Gradle, run this command:
scoopinstallgradle
Enter fullscreen mode Exit fullscreen mode

Scoop makes the mvn and gradle commands available globally. Scoop controls the system paths. These tools use the Java version that you selected with the scoop reset command.


Step 6: Automated environment variables

Many frameworks, Integrated Development Environments (IDEs), and build tools use the JAVA_HOME environment variable. This variable tells the tools where the compiler is.

When you install a JDK with Scoop, Scoop creates the JAVA_HOME environment variable. This variable points to a current symlink directory:

C:\Users\<YourUser>\scoop\apps\<jdk-package-name>\current

The scoop reset command

The JAVA_HOME variable points to the Scoop symlink. You do not change your system environment variables again.

  • When you run scoop reset openjdk17, Scoop changes the shortcut to point to the Java 17 directory.
  • Any IDE or tool that uses JAVA_HOME adapts to Java 17 when it reads the directory.

Step 7: Configure your IDEs

To make sure that your IDEs use your Scoop configuration, follow these steps.

IntelliJ IDEA

IntelliJ can detect installed SDKs. Point IntelliJ to the active directory of Scoop to keep your projects dynamic.

  1. Go to File, Project Structure, and Project.
  2. In the SDK section, click the dropdown list.
  3. Select Add SDK, and then select JDK.
  4. Go to your user directory: C:\Users\<YourUser>\scoop\apps\openjdk\current.
  5. Select the current folder. IntelliJ uses the Java version that you select with the scoop reset command.

Visual Studio Code

If you use the Extension Pack for Java, you must tell VS Code where your Java runtimes are.

  1. Press Ctrl + Shift + P to open the command palette.

  2. Select Preferences: Open User Settings (JSON). This opens your settings.json file.

  3. Add the java.jdt.ls.java.home key to the file.

  4. Set the value to your active Scoop symlink:

"java.jdt.ls.java.home":"C:/Users/<YourUser>/scoop/apps/openjdk/current"
Enter fullscreen mode Exit fullscreen mode
  1. Replace <YourUser> with your Windows username. Use forward slashes (/).

Troubleshooting and tips

Terminal restart

If you use Command Prompt (cmd.exe) or an IDE terminal, you must restart your terminal window. Do this after you switch Java versions. PowerShell sessions usually update without a restart.

Global installations

If you want Java available for all Windows user accounts, install it globally.

  1. Open your terminal as an Administrator.

  2. Add the -g flag to your command:

scoopinstall-gopenjdk17scoopreset-gopenjdk17
Enter fullscreen mode Exit fullscreen mode

Source: dev.to

arrow_back Back to Tutorials