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?

Hleb
  • 7,037
  • 12
  • 58
  • 117

3 Answers3

4

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

Huhngut
  • 402
  • 6
  • 13
  • 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 for – Bob Kuhar Sep 27 '22 at 20:14
  • 1
    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. https://github.com/FelixSelter/JEnv-for-Windows/issues/36 – Huhngut Sep 28 '22 at 18:35
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.

Stanislav
  • 27,441
  • 9
  • 87
  • 82
  • 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.. – Hleb Oct 19 '15 at 11:35
  • 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 solution – Stanislav Oct 19 '15 at 11:40
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
    
Hleb
  • 7,037
  • 12
  • 58
  • 117
  • 12
    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. – Ilya Serbis Jul 06 '18 at 18:29
  • 2
    One alternative solution for Windows could be `sdkman` https://sdkman.io/. Although it needs Mingw or the embedded Linux that W10 has – kelgwiin May 26 '20 at 09:32
  • 1
    @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. – Hleb Oct 14 '20 at 14:44
  • 1
    Just installed jEnv on top of WSL Ubuntu(https://ubuntu.com/wsl). Worked like a charm :D – marciopd Jan 15 '22 at 20:01