5
\$\begingroup\$

At my work, I need to use multiple versions of Java, depending on which project I work on. I've made a batch script to take care of this. There are several different actions that you can perform with the script.

You can change the current value of your JAVA_HOME from a version in your file. You can add new Java installation (pre-installed one), you can remove them and you can list them. You can echo your env variable, if you're not sure what's the value at the moment.

This is a script I'm using for myself for the time being, so there are a lot of things that are probably not robust or are not yet implemented. For example, you can use the program with non-existent version and it will not crash or warn you. The add and remove functions work but they output the command and can surely be played with. It does work when you know what to do.

This is the first time I'm writing a batch script, and I must say it is mostly been googling and hacking something together.

@ECHO OFF
if "%1"=="" goto empty
if "%1"=="echo" goto output
if "%1"=="e" goto output
if "%1"=="list" goto list
if "%1"=="l" goto list
if "%1"=="add" goto add
if "%1"=="a" goto add
if "%1"=="remove" goto remove
if "%1"=="r" goto remove
rem To escape '%' the sequence is '%%', so to not have to have an arg like %JAVA6%, the following will add % % on both end of the argument
SET java_version=%%%1%%
for /f "delims== tokens=1,2" %%a in (%~dp0java_versions.store) do (
 set "%%~a=%%b"
)
call :unquote JAVA_HOME %java_version%
for /f "delims== tokens=1,2" %%a in (%~dp0java_versions.store) do (
 set "%%a="
)
goto :EOF
:unquote
 set %1=%~2
 goto :output
:output
 ECHO "JAVA_HOME=%JAVA_HOME%"
 SET "java_version="
 goto :EOF
:list
 FOR /f "delims=" %%x IN (%~dp0java_versions.store) DO (
 echo "%%~x"
 )
 goto :EOF
:add
 if "%2"=="" (
 ECHO "Name of the entry missing"
 goto :EOF
 )
 if "%~3"=="" (
 ECHO "Path of the entry missing"
 goto :EOF
 )
 echo %~3
 IF EXIST %~3 (
 echo %2="%~3">> %~dp0java_versions.store
 ECHO %2="%~3" added
 ) ELSE (
 ECHO %3 is not an actual folder
 ) .
 goto :EOF
:remove
 if "%2"=="" (
 ECHO "Name of the entry missing as an argument of the batch"
 goto :EOF
 )
 set "found="
 for /f "delims== tokens=1,2" %%a in (%~dp0java_versions.store) do (
 if "%2"=="%%a" (
 set found=1
 echo found
 )
 )
 if defined found (
 for /f "delims== tokens=1,2" %%a in (%~dp0java_versions.store) do (
 if NOT "%2"=="%%a" (
 echo %%~a=%%b>> %~dp0new_java_versions.store
 )
 )
 xcopy %~dp0new_java_versions.store %~dp0java_versions.store /y
 del %~dp0new_java_versions.store /f /q
 ECHO Entry %2 deleted
 )
 goto :EOF
:empty
 ECHO "Empty argument, please provide an argument"
 goto :EOF

Store file format :

JAVA8_11="C:\Program Files\Java\jdk1.8.0_11"
JAVA8="C:\Program Files\Java\jdk1.8.0_73"
JAVA7="C:\Program Files\Java\jdk1.7.0_51"
Phrancis
20.5k6 gold badges69 silver badges155 bronze badges
asked Aug 24, 2016 at 23:12
\$\endgroup\$
0

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.