About
This guide fixes an error where IntelliJ says it cannot find Java when you build a project locally. It only applies if you run IntelliJ on Windows and use WSL (Windows Subsystem for Linux) as your run environment.
Note: You might have to do this for every project you want to build locally, because these settings are saved per project.
Why does this happen?
When you use WSL, your project and your build tools run inside a small Linux system on your computer. IntelliJ itself runs on Windows. If IntelliJ picks a Java installation from Windows, the build running in Linux cannot find it. The fix is to tell IntelliJ to use a Java installation that lives inside WSL.
Words you will see
- WSL: a Linux system that runs inside Windows.
- JDK: the Java Development Kit, the software that is needed to build Java programs.
- Module: a part of your project. Most small projects have just one.
Steps
1. Open the compiler settings
In IntelliJ, click File, then Settings. Go to Build, Execution, Deployment, then Compiler, then Java Compiler.
2. Turn off the module JDK option
Find Use compiler from module target JDK when possible and remove the tick from the box. Click Apply, then OK. This stops IntelliJ from choosing a JDK on its own, which can be the wrong one.
3. Open Project Structure
Click File, then Project Structure. In the menu on the left, click Modules.
4. Choose a Java from WSL
Select your module. Find the Module SDK list and choose Java 17, or the version you need.
Important: The Java you choose must be one that is installed inside WSL. In the list it usually has a path that starts with
\\wsl$\or\\wsl.localhost\. If you pick one with a normal Windows path likeC:\Program Files\..., the error will stay.
5. Save and try again
Click Apply, then OK. Build your project again.
You are done! 😄
Sources
- JetBrains’ guide to developing in WSL: https://www.jetbrains.com/help/idea/establish-wsl-connection.html
- JetBrains’ guide to Java compiler settings: https://www.jetbrains.com/help/idea/java-compiler.html
- JetBrains’ guide to Project Structure and modules: https://www.jetbrains.com/help/idea/project-structure-dialog.html