How to Use Multiple Java Versions in Windows: A Simple Tutorial

How to Use Multiple Java Versions in Windows: A Simple Tutorial

Blog Author: Anup G.

Managing multiple Java versions on Windows can be challenging, but jEnv simplifies it by allowing you to switch between various Java versions seamlessly. This tutorial will guide you through the step-by-step installation of jEnv on Windows.

Step 1: Install Java (If Not Installed)

Before installing jEnv, ensure you have Java installed on your system. You can download Java from:

After installation, verify Java installation by running:

java -version        

If Java is installed correctly, it will display the installed version.

Step 2: Install Git Bash (Recommended)

Since jEnv is primarily Unix-based, using Git Bash provides a better shell-like environment on Windows.

The jEnv only works on Unix based terminals.        

  1. Download Git for Windows from git-scm.com.
  2. Install it and select “Git Bash” as the terminal option during installation.

Step 3: Installing jEnv Manually

Since jEnv does not officially support Windows, you need to install it manually using Git.

Open Git Bash and run:

git clone https://github.com/jenv/jenv.git ~/.jenv        
Article content

Add jEnv to the system PATH by modifying the ~/.bashrc file:

echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(jenv init -)"' >> ~/.bashrc        
Article content

Apply the changes:

source ~/.bashrc        
Article content

If you see output as above, follow the Step 7 of the guide.

Step 4: Confirm jEnv Installation

To confirm jEnv is installed successfully, run:

jenv - version        
Article content

If jEnv is installed correctly, it will display its version.

Step 5: Add Java Versions to jEnv

Now, manually add installed Java versions to jEnv.

Locate installed Java versions on your machine:

ls /c/Program\ Files/Java/        

Add a Java version to jEnv:

jenv add "C:\Program Files\Java\jdk-XX.X.X"        
Article content

(Replace jdk-XX.X.X with the actual installed version.) Set a global Java version: (Ensure to use exact names as shown when we run jenv versions)

jenv global jdk-XX.X.X        
Article content

Set a local Java version for a specific project:

cd /path/to/project
jenv local jdk-XX.X.X        
Article content

Verify the configured Java versions:

jenv versions        
Article content

Step 6: Enable jEnv Plugins (Optional)

To automatically set Java environment variables, enable the export plugin:

jenv enable-plugin export        
Article content
source ~/.bashrc        
Article content

Step 7: Fix jEnv Path Issues

If you encounter issues like “Java binary in path is not in the jenv shims”, follow these steps:

Check the content of ~/.jenv/bin/jenv:

cat ~/.jenv/bin/jenv        

It should output:

../libexec/jenv        
Article content

If needed, modify the file using:

nano ~/.jenv/bin/jenv        
Article content

Ensure it contains:

#!/usr/bin/env bash
exec "$(dirname "$0")/../libexec/jenv" "$@"        
Article content

Reload the environment:

source ~/.bashrc        
Article content

Verify jEnv status:

jenv doctor        
Article content

If you see errors like:

[ERROR] Java binary in path is not in the jenv shims.        

Try adding Java manually:

jenv add /c/Program\ Files/Java/jdk-XX.X.X        
Article content

Step 8: Troubleshooting (If Needed)

If jEnv does not detect Java versions, try the following:

Ensure JAVA_HOME is correctly set in your ~/.bashrc:

echo 'export JAVA_HOME="$(jenv prefix system)"' >> ~/.bashrc        
source ~/.bashrc        

  • Restart your terminal after making changes.
  • Run jenv doctor to check for any configuration issues.

Conclusion

Congratulations! You have successfully installed jEnv on Windows. You can now effortlessly switch between multiple Java versions as needed.

If you found this tutorial helpful, feel free to share it! Need help? Leave a comment below!

To view or add a comment, sign in

Others also viewed

Explore topics