Hleb
Hleb

Reputation: 7391

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?

Upvotes: 7

Views: 12508

Answers (3)

Huhngut
Huhngut

Reputation: 492

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

Upvotes: 6

Hleb
Hleb

Reputation: 7391

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
    

Upvotes: 0

Stanislav
Stanislav

Reputation: 28126

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.

Upvotes: 1

Related Questions