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

  1. Open Notepad++:

    • Launch Notepad++ from your start menu or desktop shortcut.
  2. Start a New File:

    • Click on File in the top menu bar.
    • Select New or press Ctrl + N to open a new document.
  3. 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 pressing Ctrl + V.

  4. 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\
  5. Save the File with a .bat Extension:

    • Click on File in the top menu bar.
    • Select Save As....
    • In the Save as type dropdown, select All Types.
    • In the File name field, type switch-java.bat.
    • Choose a location where you want to save the batch file (e.g., Desktop or Documents).
    • Click Save.
  6. 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.

Step 3: Use the Batch File

  1. 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.
  2. 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):
      
  3. 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.
  4. Verify the Switch:

    • The script will update your JAVA_HOME and PATH environment variables to point to the selected Java version.
    • You will see a confirmation message indicating which Java version has been activated.

Step 4: Set JAVA_HOME on Windows 10 and 11

  1. Open System Properties:

    • Press Windows + Pause/Break to open System Properties, or right-click on This PC and select Properties.
  2. Open Advanced System Settings:

    • Click on Advanced system settings on the left side.
  3. Open Environment Variables:

    • In the System Properties window, click on the Environment Variables button at the bottom.
  4. Create or Update JAVA_HOME Variable:

    • In the Environment Variables window, under System variables, click New if JAVA_HOME does not exist, or select JAVA_HOME and click Edit 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.
  5. Update PATH Variable:

    • In the Environment Variables window, under System variables, find and select the Path variable, then click Edit.
    • In the Edit Environment Variable window, click New and add %JAVA_HOME%\bin.
    • Click OK to save the changes.
  6. 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.

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:

  1. Open Command Prompt:

    • Press Windows + R to open the Run dialog box.
    • Type cmd and press Enter, or click OK.
  2. 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.
  3. Verify JAVA_HOME:

    • To check if the JAVA_HOME environment variable is set, type the following command and press Enter:
      echo %JAVA_HOME%
      
    • This command will display the path set for JAVA_HOME. If JAVA_HOME is not set, you will see an empty line or an error message.
  4. 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.

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.