Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching between two java versions on Windows

Currently I have a java project where I should support different version of it, which use different version of Java (and some tools, like Ant). Depends on issue tickets I need to handle both java version (7 and 8) and pretty often switch between them. Can someone suggest the best way to handle it easier? I'm working on Windows 7, so I wrote such bat-file for switching ("switch_java.bat"):

@ECHO OFF
set changeToNewVersion=%1

IF "%changeToNewVersion%"=="true" (
    setx /M ANT_HOME "c:\Program Files\Ant\apache-ant-1.9.4"
    setx /M JAVA_HOME "c:\Program Files\Java\jdk1.8.0_51"
) ELSE IF "%changeToNewVersion%"=="false" (
    setx /M ANT_HOME "c:\Program Files\Ant\apache-ant-1.8.3"
    setx /M JAVA_HOME "c:\Program Files\Java\jdk1.7.0_79"
) ELSE (
    echo ERROR: Enter key!
)

But maybe there is more optimal solution?

like image 473
Gleb Avatar asked Jun 26 '26 13:06

Gleb


1 Answers

There is a Github tool for windows. I'm using it myself and it's really nice.
The maintainer is normally responding very fast if you have a problem

Usage (Note: local overwrites change. use overwrites local)

  1. Add a new Java environment (requires absolute path)
    jenv add <name> <path>
    Example: jenv add jdk15 D:\Programme\Java\jdk-15.0.1

  2. Change your java version for the current session
    jenv use <name>
    Example: jenv use jdk15
    Environment var for scripting:
    ---PowerShell: $ENV:JENVUSE="jdk17"
    ---CMD/BATCH: set "JENVUSE=jdk17"

  3. Clear the java version for the current session
    jenv use remove
    Example: jenv use remove
    Environment var for scripting:
    ---PowerShell: $ENV:JENVUSE=$null
    ---CMD/BATCH: set "JENVUSE="

  4. Change your java version globally
    jenv change <name>
    Example: jenv change jdk15

  5. Always use this java version in this folder
    jenv local <name>
    Example: jenv local jdk15

  6. Clear the java version for this folder
    jenv local remove
    Example: jenv local remove

  7. List all your Java environments
    jenv list
    Example: jenv list

  8. Remove an existing JDK from the JEnv list
    jenv remove <name>
    Example: jenv remove jdk15

  9. Enable the use of javac, javaw or other executables sitting in the java directory
    jenv link <Executable name>
    Example: jenv link javac

  10. Uninstall jenv and automatically restore a Java version of your choice
    jenv uninstall <name>
    Example: jenv uninstall jdk17

  11. Automatically search for java versions to be added
    jenv autoscan ?<path>?
    Example: jenv autoscan "C:\Program Files\Java"
    Example: jenv autoscan // Will search entire system

https://github.com/FelixSelter/JEnv-for-Windows

like image 144
Huhngut Avatar answered Jun 29 '26 04:06

Huhngut