jabba icon indicating copy to clipboard operation
jabba copied to clipboard

Support for traditional command prompt on Windows

Open rubin55 opened this issue 7 years ago • 10 comments

Currently, jabba has a powershell wrapper that uses the --fd3 "hidden" option to write a temporary powershell script with export statements in it for PATH, JAVA_HOME and JAVA_HOME_BEFORE_JABBA.

It would be much awesome if, next to jabba.ps1, there would be a jabba.bat that can be used from the regular cmd.exe command prompt. Looking at it roughly, replace export with set and change the quoting (you would quote the whole, like so: set "WHAT=is this crazy batch stuff").

rubin55 avatar Jun 21 '17 08:06 rubin55

Quick fix which translates powershell compatible output from --fd3 to batch compatible format and "sources" into the current shell:

@echo off

:: Set environment variables.
set "JABBA_HOME=%USERPROFILE%\.jabba"
set "TEMP_ENV=%TEMP%\jabba.env"
set "TEMP_REP=%TEMP%\jabba.rep"
set "TEMP_BAT=%TEMP%\jabba.bat"

:: Run jabba, write to TEMP_ENV.
"%JABBA_HOME%\bin\jabba.exe" %1 %2 %3 %4 %5 --fd3 "%TEMP_ENV%"

if exist "%TEMP_ENV%" (
    :: Convert from powershell to batch environment schript.
    powershell -Command "(gc %TEMP_ENV%) -replace 'export ', 'set \"' ^| Out-File -encoding ASCII %TEMP_REP%"
    powershell -Command "(gc %TEMP_REP%) -replace '=\"', '=' ^| Out-File -encoding ASCII %TEMP_BAT%"

    :: Exectute within current shell (i.e, no call).
    "%TEMP_BAT%"

    :: Clean up files.
    del "%TEMP_ENV%" "%TEMP_REP%" "%TEMP_BAT%"
)

:: Unset used environment variables.
set JABBA_HOME=
set TEMP_ENV=
set TEMP_REP=
set TEMP_BAT=

rubin55 avatar Jun 21 '17 09:06 rubin55

This would be awesome! We would need to put jabba.bat on the PATH somehow but other than that it looks like something worth doing. Any chance you would be interested in sending in the PR ("develop" branch)? I'd gladly merge it in.

shyiko avatar Jun 21 '17 17:06 shyiko

https://github.com/shyiko/jabba/pull/86 merged in. I'll close this ticket as soon as changes are in master.
@rubin55 Thanks again for the PR!

shyiko avatar Jun 22 '17 20:06 shyiko

:) Awesome!

rubin55 avatar Jun 22 '17 20:06 rubin55

I'm not exactly sure what the reason is to not use CALL here:

    :: Exectute within current shell (i.e, no call).
    "%TEMP_BAT%"

I noticed some minor strange problems for me that the title of the command line appends the command after each call to jabba use x.x. I tried to search for the reasons and I found it to be the missing CALL. I'm not an expert at batch scripts and I tried to research a bit to find out what the implications are to use CALL or not but couldn't really find a good explanation. Only that it seems to be the recommended approach to use CALL when calling a batch script from a batch script. So I changed my jabba.bat to use CALL and the title problem went away. Since then I'm pretty happy with it. Maybe it's worth reconsidering the missing CALL.

gregorko avatar Oct 08 '18 18:10 gregorko

I did some further modifications to the batch script. Mainly because I'm regularly seeing errors like (freely translated): Path "C:\Users\Gregor\AppData\Local\Temp\jabba_.bat" can't be accessed because it is used by another process. or also Path "C:\Users\Gregor\AppData\Local\Temp\jabba_.bat" can't be accessed because its not available.

This happens when jabba.bat is called in parallel on multiple consoles. Therefore my desire was to use a unique temporary file name per call. Also I was not very happy about the multiple intermediate files jabba creates. Additionally I addressed the unset issue on jabba deactivate that was not translated the old script.

I came up with the following optimization of jabba.bat which works quite good for me. I hope it is of use for you.

@echo off

:: Set environment variables.
set "JABBA_HOME=%USERPROFILE%\.jabba"
FOR /F %%a IN ('POWERSHELL -C "$([guid]::NewGuid().ToString())"') DO (SET JABBA_GUID=%%a)
set "TEMP_ENV=%TEMP%\jabba_%JABBA_GUID%.env"

:: Run jabba, write to TEMP_ENV.
"%JABBA_HOME%\bin\jabba.exe" %1 %2 %3 %4 %5 --fd3 "%TEMP_ENV%"

if exist "%TEMP_ENV%" (
    FOR /F "usebackq delims=" %%a IN (`powershell -C "(gc %TEMP_ENV%) -replace 'export ', 'set \"' -replace '^=\"', '=' -replace 'unset ([A-Z_]*)', 'set \"$1^=\"' "`) DO %%a
    
    :: Clean up files.
    del "%TEMP_ENV%"
)

:: Unset used environment variables.
set JABBA_HOME=
set TEMP_ENV=
set JABBA_GUID=

gregorko avatar Nov 10 '18 17:11 gregorko

Thank you, @gregorko. I appreciate it.

shyiko avatar Nov 11 '18 04:11 shyiko

What's the status of Command Prompt (cmd) support?

Yesterday I installed jabba on Win 10 via the Powershell script. Then I noticed there's a .jabba/bin/jabba.exe in my home dir. I added that to my PATH and it looks like I can run jabba via cmd but it doesn't seem to work. jabba ls works for example, but if I do jabba use <version> and then jabba current I just get empty result and it seems non functional. This seems to work fine in Powershell though.

Would be nice to be able to use jabba easily in normal cmd (or some other terminal like the new Windows Terminal) without using Powershell since I HATE Powershell.

Well actually I prefer running CLI stuff in WSL or on a real Linux system where jabba works nicely (my main working OS is Pop!_OS) but once in a while I need to do some testing on Windows where I'd like to use the common environment (cmd) to debug and reproduce issues in Java software and it would be great if I could use jabba there too to easily switch between Java versions.

Haprog avatar Feb 19 '21 07:02 Haprog

Just merged #534!

willcohen avatar Feb 19 '21 15:02 willcohen

Looks like that merge broke the install/upgrade script. See my comment here.

Haprog avatar Feb 22 '21 11:02 Haprog