Confusing Java Versions in WSL2 with IntelliJ IDE
If you are running IntelliJ IDE on WSL2 (Windows Subsystem for Linux version 2), you might have encountered issues with Java versions, especially when working with the command line. Common examples include typing <code>java.exe >/mnt/c/ProgramFiles/Java/j...</code> in the terminal.
Understanding Java Versions in WSL2
WSL2 runs a Linux distribution, such as Ubuntu, alongside Windows. When installing Java on WSL2, you usually install the OpenJDK or Oracle JDK package specific to the Linux distribution. These installations don't interfere with any Java installations on Windows.
However, IntelliJ IDE installed on Windows may use a different Java version than what you installed on WSL2. That is because IntelliJ IDE picks up the Java version available in the Windows %PATH% and %JAVA_HOME% variables, which may or may not point to the WSL2 Java installation.
Checking Java Versions in WSL2 and IntelliJ IDE
To check the Java version in your WSL2 Ubuntu terminal:
java -version
To check the Java version in IntelliJ IDE:
- Open IntelliJ IDE.
- Go to
Help > Find Action. - Type
Switch Boot JDKand press Enter. - The current Java version used by IntelliJ IDE will be displayed.
Updating Java Versions in WSL2 and IntelliJ IDE
To update the Java version in WSL2:
- Update the Linux distribution's package manager.
- Install the desired Java version, e.g., for Ubuntu:
sudo apt update
sudo apt install openjdk-11-jdk
To update the Java version in IntelliJ IDE:
- Install the desired Java version on Windows, if not already installed.
- In IntelliJ IDE, go to
File > Settings. - Go to
Build, Execution, Deployment > Build Tools > Gradle. - Update the
Gradle JDKpath to the new Java version on Windows. - Update the
Project SDKandLanguage Levelto the new Java version in the sameSettingswindow.
Configuring Java Versions in WSL2 and IntelliJ IDE
To configure the Java version in WSL2:
- Modify the
PATHandJAVA_HOMEvariables in the WSL2 terminal. - Update the
.bashrcor.zshrcfile in your WSL2 home directory.
To configure the Java version in IntelliJ IDE:
- Use the
Switch Boot JDKoption in IntelliJ IDE to switch between available Java versions as needed.
Running IntelliJ IDE on WSL2 can lead to confusion regarding Java versions. Understand the differences between Java installations on WSL2 and Windows. Check, update, and configure Java versions in both WSL2 and IntelliJ IDE as required.
References
- Oracle JDK: https://www.oracle.com/java/technologies/javase-jdk11-downloads.html
- OpenJDK: https://openjdk.org/
- Gradle: https://gradle.org/install/