Switching between two java versions on Windows
Asked Answered
S

3

6

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?

Source answered 19/10, 2015 at 11:16 Comment(0)
S
0

For managing different versions of Java on one environment we can use jEnv tool. After installing and adding it to Path environment variable all that need to do are:

  • Add all Java versions that you need to jEnv config, like:

    jenv add c:\Program Files\Java\jdk1.7.0_80
    
  • Configure which JVM to use (globally, by directory or for the current shell instance):

    jenv global jdk1.7.0_80
    
Source answered 26/7, 2017 at 20:35 Comment(4)
jEnv looks like a great tool but it is for Unix-based OSs only. It is written on shell script and doesn't work in Windows environment.Kellykellyann
One alternative solution for Windows could be sdkman sdkman.io. Although it needs Mingw or the embedded Linux that W10 hasDidactics
@Lu55, that's probably to late to answer to you, but now on Windows you can use jEnv with WSL, or just use Cygwin to bring bash to Windows environment.Source
Just installed jEnv on top of WSL Ubuntu(ubuntu.com/wsl). Worked like a charm :DLevileviable
J
5

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

Joettejoey answered 26/3, 2021 at 17:8 Comment(2)
you don't know if the JEnv-for-Windows thing respects the .java-version file in a directory, do you? That is basically what I'm looking forRecrystallize
This is a feature that's been worked on at the moment. There's an open Pull Request. As the contributor is currently not responding the owner plans to implement this soon. For now, you have to add the version from the version file to a jenv local and it will work the same. Just one command more to be executed once which should even be resolved soon. Maybe you want to subscribe to the issue. github.com/FelixSelter/JEnv-for-Windows/issues/36Joettejoey
P
1

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.

Piccadilly answered 19/10, 2015 at 11:27 Comment(2)
Thanks for your response, I think, I'll split my script into two parts, for each version separately. But I'm looking for more "standard" solutions, if such exists..Source
Yes, I see, but as I know, batch scripts are exactly the common solution for that. But would be interesting to see any other possible solutionPiccadilly
S
0

For managing different versions of Java on one environment we can use jEnv tool. After installing and adding it to Path environment variable all that need to do are:

  • Add all Java versions that you need to jEnv config, like:

    jenv add c:\Program Files\Java\jdk1.7.0_80
    
  • Configure which JVM to use (globally, by directory or for the current shell instance):

    jenv global jdk1.7.0_80
    
Source answered 26/7, 2017 at 20:35 Comment(4)
jEnv looks like a great tool but it is for Unix-based OSs only. It is written on shell script and doesn't work in Windows environment.Kellykellyann
One alternative solution for Windows could be sdkman sdkman.io. Although it needs Mingw or the embedded Linux that W10 hasDidactics
@Lu55, that's probably to late to answer to you, but now on Windows you can use jEnv with WSL, or just use Cygwin to bring bash to Windows environment.Source
Just installed jEnv on top of WSL Ubuntu(ubuntu.com/wsl). Worked like a charm :DLevileviable

© 2022 - 2024 — McMap. All rights reserved.