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)
Add a new Java environment (requires absolute path)
jenv add<name> <path>
Example:jenv add jdk15 D:\Programme\Java\jdk-15.0.1Change 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"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="Change your java version globally
jenv change<name>
Example:jenv change jdk15Always use this java version in this folder
jenv local<name>
Example:jenv local jdk15Clear the java version for this folder
jenv local remove
Example:jenv local removeList all your Java environments
jenv list
Example:jenv listRemove an existing JDK from the JEnv list
jenv remove<name>
Example:jenv remove jdk15Enable the use of javac, javaw or other executables sitting in the java directory
jenv link<Executable name>
Example:jenv link javacUninstall jenv and automatically restore a Java version of your choice
jenv uninstall<name>
Example:jenv uninstall jdk17Automatically 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
Answer from Huhngut on Stack OverflowVideos
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)
Add a new Java environment (requires absolute path)
jenv add<name> <path>
Example:jenv add jdk15 D:\Programme\Java\jdk-15.0.1Change 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"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="Change your java version globally
jenv change<name>
Example:jenv change jdk15Always use this java version in this folder
jenv local<name>
Example:jenv local jdk15Clear the java version for this folder
jenv local remove
Example:jenv local removeList all your Java environments
jenv list
Example:jenv listRemove an existing JDK from the JEnv list
jenv remove<name>
Example:jenv remove jdk15Enable the use of javac, javaw or other executables sitting in the java directory
jenv link<Executable name>
Example:jenv link javacUninstall jenv and automatically restore a Java version of your choice
jenv uninstall<name>
Example:jenv uninstall jdk17Automatically 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
IMO, it's primarily opinion-based question, but I don't think, that you may find a better solution, then a batch scripts to do that.
As from my point of view, it could be not very usefull to make a script with parameters, because it should be executed via command line or from another bat file.
So, you can create 2 separate bat files, one to set jdk 1.7 and the second is to set jdk 1.8. Or you can modify your script, to determine the current version and set another one. In both cases, you can simply call execute a bat file without providing any additional parameters.