Switching Between Oracle Java 8 and Adoptium Java 11 and 17 on Windows Using a Batch File - MegaMek/megamek GitHub Wiki
THIS PAGE IS STILL IN DEVELOPMENT AND ANY INFORMATION ON IT SHOULD BE IGNORED FOR NOW
Switching Between Oracle Java 8 and Adoptium Java 17 on Windows Using a Batch File
Before beginning, please note that these directions require a comfort level with creating batch files and installing different versions of Java. While we have tried to provide detailed instructions, we cannot offer support for every possible combination of setups. Java 17 should be backwards compatible with Java 8, but specific applications may still require Java 8.
MegaMek Java Requirements
Program Version | Minimum Java Version Required | Java Version Recommended | Recommendation |
---|---|---|---|
0.49.7-MILESTONE | Java 11 | Java 11 | Upgrade to 0.49.19.1-MILESTONE and Java 17 |
0.49.11-Development or later | Java 17 | Java 17 | Upgrade to 0.49.19.1-MILESTONE and Java 17 |
0.49.19.1-MILESTONE | Java 11 | Java 17 | Current Recommended Version. Ensure you're running Java 17 |
0.48.0-Stable | Java 8 | Java 17 | No longer supported. Upgrade to 0.49.19.1-MILESTONE |
0.50.0-Development (Coming Soon) | Java 17 | Java 17 | This Development Cycle hasn't started yet |
Managing multiple Java versions on a single machine can be a challenge, especially when different applications require different versions. Here’s a step-by-step guide to help you set up a batch file that lets you switch between Oracle Java 8, Adoptium Java 11, and Adoptium Java 17 effortlessly.
Step 1: Install Java Versions
1. Download and Install Oracle Java 8
- Download Oracle Java 8 from the Oracle website.
- Follow the installation instructions and note the installation path (
C:\Program Files\Java\jre1.8.0_301
).
2. Download and Install Adoptium Java 11
- Download Adoptium Java 11 from the Adoptium website.
- Follow the installation instructions and note the installation path (
C:\Program Files\Eclipse Adoptium\jre-11.0.11.1-hotspot\
).
3. Download and Install Adoptium Java 17
- Download Adoptium Java 17 from the Adoptium website.
- Follow the installation instructions and note the installation path (
C:\Program Files\Eclipse Adoptium\jre-17.1.01.9-hotspot\
).
Step 2: Create the Batch File
-
Open Notepad++:
- Launch Notepad++ from your start menu or desktop shortcut.
-
Start a New File:
- Click on
File
in the top menu bar. - Select
New
or pressCtrl + N
to open a new document.
- Click on
-
Copy and Paste the Script:
-
Copy the following script:
@echo off setlocal :menu echo ===================================== echo Choose the Java version to switch to: echo ===================================== echo 1. Oracle Java 8 (64-bit JRE) echo 2. Adoptium Java 11 (64-bit JRE) echo 3. Adoptium Java 17 (64-bit JRE) echo 4. Exit echo ===================================== echo For MegaMek 0.48.0 Stable: Enter 1 to switch to Oracle Java 8 (64-bit JRE). echo For MegaMek 0.49.7-MILESTONE: Enter 2 to switch to Adoptium Java 11 (64-bit JRE). echo For MegaMek 0.49.11-Development or later: Enter 3 to switch to Adoptium Java 17 (64-bit JRE). echo For MegaMek 0.49.19.1-MILESTONE: Enter 3 to switch to Adoptium Java 17 (64-bit JRE). echo For MegaMek 0.50.0-Development (Coming Soon): Enter 3 to switch to Adoptium Java 17 (64-bit JRE). echo ===================================== set /p choice=Enter your choice (1, 2, 3, or 4): if "%choice%"=="1" goto set_java8 if "%choice%"=="2" goto set_java11 if "%choice%"=="3" goto set_java17 if "%choice%"=="4" goto end echo Invalid choice, please try again. goto menu :set_java8 rem Confirm install path of each Java set JAVA_HOME=C:\Program Files\Java\jre1.8.0_301 set PATH=%JAVA_HOME%\bin;%PATH% echo Switched to Oracle Java 8 (64-bit JRE) goto end :set_java11 rem Confirm install path of each Java set JAVA_HOME=C:\Program Files\Eclipse Adoptium\jre-11.0.11.1-hotspot\ set PATH=%JAVA_HOME%\bin;%PATH% echo Switched to Adoptium Java 11 (64-bit JRE) goto end :set_java17 rem Confirm install path of each Java set JAVA_HOME=C:\Program Files\Eclipse Adoptium\jre-17.1.01.9-hotspot\ set PATH=%JAVA_HOME%\bin;%PATH% echo Switched to Adoptium Java 17 (64-bit JRE) goto end :end pause endlocal
-
Paste the script into the new document in Notepad++ by right-clicking and selecting
Paste
or pressingCtrl + V
.
-
-
Modify the Paths:
- Ensure the paths are set as follows:
- For Java 8:
C:\Program Files\Java\jre1.8.0_301
- For Java 11:
C:\Program Files\Eclipse Adoptium\jre-11.0.11.1-hotspot\
- For Java 17:
C:\Program Files\Eclipse Adoptium\jre-17.1.01.9-hotspot\
- For Java 8:
- Ensure the paths are set as follows:
-
Save the File with a .bat Extension:
- Click on
File
in the top menu bar. - Select
Save As...
. - In the
Save as type
dropdown, selectAll Types
. - In the
File name
field, typeswitch-java.bat
. - Choose a location where you want to save the batch file (e.g., Desktop or Documents).
- Click
Save
.
- Click on
-
Verify the Saved File:
- Navigate to the location where you saved
switch-java.bat
. - Ensure the file has a
.bat
extension and is not saved as a.txt
file.
- Navigate to the location where you saved
Step 3: Use the Batch File
-
Run the Batch File:
- Double-click on the
switch-java.bat
file to run it. - If prompted by Windows User Account Control, click
Yes
to allow the program to run.
- Double-click on the
-
Interact with the Menu:
-
A command prompt window will open with a menu similar to the following:
===================================== Choose the Java version to switch to: ===================================== 1. Oracle Java 8 (64-bit JRE) 2. Adoptium Java 11 (64-bit JRE) 3. Adoptium Java 17 (64-bit JRE) 4. Exit ===================================== For MegaMek 0.48.0 Stable: Enter 1 to switch to Oracle Java 8 (64-bit JRE). For MegaMek 0.49.7-MILESTONE: Enter 2 to switch to Adoptium Java 11 (64-bit JRE). For MegaMek 0.49.11-Development or later: Enter 3 to switch to Adoptium Java 17 (64-bit JRE). For MegaMek 0.49.19.1-MILESTONE: Enter 3 to switch to Adoptium Java 17 (64-bit JRE). For MegaMek 0.50.0-Development (Coming Soon): Enter 3 to switch to Adoptium Java 17 (64-bit JRE). ===================================== Enter your choice (1, 2, 3, or 4):
-
-
Make a Selection:
- For MegaMek 0.48.0 Stable: Enter
1
to switch to Oracle Java 8 (64-bit JRE). - For MegaMek 0.49.7-MILESTONE: Enter
2
to switch to Adoptium Java 11 (64-bit JRE). - For MegaMek 0.49.11-Development or later: Enter
3
to switch to Adoptium Java 17 (64-bit JRE). - For MegaMek 0.49.19.1-MILESTONE: Enter
3
to switch to Adoptium Java 17 (64-bit JRE). - For MegaMek 0.50.0-Development (Coming Soon): Enter
3
to switch to Adoptium Java 17 (64-bit JRE). - Press
Enter
after making your choice.
- For MegaMek 0.48.0 Stable: Enter
-
Verify the Switch:
- The script will update your
JAVA_HOME
andPATH
environment variables to point to the selected Java version. - You will see a confirmation message indicating which Java version has been activated.
- The script will update your
Step 4: Set JAVA_HOME on Windows 10 and 11
-
Open System Properties:
- Press
Windows + Pause/Break
to open System Properties, or right-click onThis PC
and selectProperties
.
- Press
-
Open Advanced System Settings:
- Click on
Advanced system settings
on the left side.
- Click on
-
Open Environment Variables:
- In the System Properties window, click on the
Environment Variables
button at the bottom.
- In the System Properties window, click on the
-
Create or Update JAVA_HOME Variable:
- In the Environment Variables window, under
System variables
, clickNew
if JAVA_HOME does not exist, or select JAVA_HOME and clickEdit
to update it. - Enter
JAVA_HOME
as the Variable name. - Enter the path to your Java installation (e.g.,
C:\Program Files\Java\jre1.8.0_301
for Java 8) as the Variable value. - Click
OK
to save the changes.
- In the Environment Variables window, under
-
Update PATH Variable:
- In the Environment Variables window, under
System variables
, find and select thePath
variable, then clickEdit
. - In the Edit Environment Variable window, click
New
and add%JAVA_HOME%\bin
. - Click
OK
to save the changes.
- In the Environment Variables window, under
-
Verify the Changes:
- Open a new Command Prompt window and type
echo %JAVA_HOME%
to verify that the JAVA_HOME variable is set correctly. - Type
java -version
to ensure that the correct Java version is being used.
- Open a new Command Prompt window and type
For more detailed instructions, you can refer to this guide.
Checking the Java Version via Command Line on Windows 10 and 11
To check the installed Java version on Windows 10 or 11, you can use the Command Prompt. Follow these steps:
-
Open Command Prompt:
- Press
Windows + R
to open the Run dialog box. - Type
cmd
and pressEnter
, or clickOK
.
- Press
-
Check the Java Version:
- In the Command Prompt window, type the following command and press
Enter
:java -version
- This command will display the currently installed Java version.
- In the Command Prompt window, type the following command and press
-
Verify JAVA_HOME:
- To check if the
JAVA_HOME
environment variable is set, type the following command and pressEnter
:echo %JAVA_HOME%
- This command will display the path set for
JAVA_HOME
. IfJAVA_HOME
is not set, you will see an empty line or an error message.
- To check if the
-
Check Installed Java Versions:
- To see all installed Java versions on your system, navigate to the directory where Java is typically installed by typing the following command and pressing
Enter
:dir "C:\Program Files\Java"
- This command will list all Java directories installed in the specified path.
- To see all installed Java versions on your system, navigate to the directory where Java is typically installed by typing the following command and pressing
By following these steps, you can easily verify which version of Java is currently active and check the installed Java versions on your Windows 10 or 11 system.