3

I follow to this https://sdkman.io/install to install install sdkman. After instillation all worked nice. I can see correct version by sdk version. Also I can install various Java version. But all are for the correct shell session. As soon as I close and open new shell, I can't get neither sdkman nor those installed JDK. I can see all JDK at ~/.sdkman/candidates/java. I have to run this all the time source "$HOME/.sdkman/bin/sdkman-init.sh"

How do I set sdkman as well as JDK permanent in my system.

Masi Boo
  • 635
  • 1
  • 10
  • 24

2 Answers2

3

When you run sdkman, at some point it printed some instructions to add a couple of lines to your shell rc file, so it would be activated with all shells.

For example, I have this at the end of my ~/.zshrc (might differ from bash)

export SDKMAN_DIR="$HOME/.sdkman"
[[ -s "$HOME/.sdkman/bin/sdkman-init.sh" ]] && source "$HOME/.sdkman/bin/sdkman-init.sh"
Augusto
  • 28,839
  • 5
  • 58
  • 88
-1

Wrote a python program for this. This will print the commands that you need to run to point your local JDKs as unclassified Vendor but allows you to reuse the JDKs present usually at /Users/{username}/Library/Java/JavaVirtualMachines directory, which would have been the case before you installed SDKMan.

After running the program as shown in image below your local JDKs will be linked with SDKMan.

"""
What this program does? Links locally available java directories inside /Users/{username}/Library/Java/JavaVirtualMachines directory to sdkman local installations.
https://i.imgur.com/NaSXX99.png
"""


import os
import subprocess
import getpass

def nameValidation(i):
    j = ''
    if len(i) > 15:
            shortName = i.split('-')
            # print(f'{shortName[0]} : {shortName}')
            if(shortName[0] == 'corretto'):
                shortName[0] = 'amzn'
            elif shortName[0] == 'temurin':
                shortName[0] = 'tem'
            elif shortName[0] == 'gluon':
                shortName[0] = 'gln'
            elif shortName[0] == 'graalvm':
                shortName[0] = 'grl'
            elif shortName[0] == 'java.net':
                shortName[0] = 'open'
            elif shortName[0] == 'liberica':
                shortName[0] = 'librca'
            elif shortName[0] == 'libericanik':
                shortName[0] = 'nik'
            elif shortName[0] == 'microsoft':
                shortName[0] = 'ms'
            elif shortName[0] == 'oracle':
                shortName[0] = 'oracle'
            elif shortName[0] == 'sapmachine':
                shortName[0] = 'sapmchn'
            elif shortName[0] == 'semeru':
                shortName[0] = 'sem'
            elif shortName[0] == 'zulu':
                shortName[0] = 'zulu'
            j = f'{shortName[1]}-{shortName[0]}'
            return j
    else:
        return i
        
    


def printCommandsToLinkJDKWithSDKMan(jdkList,baseDirectoryAddress):
    listToReturn = []
    for i in jdkList:
        name = nameValidation(i)
        listToReturn.append(f'sdk install java {name} {baseDirectoryAddress}/{i}')
        # print(f'Adding: {listToReturn[-1]}')
    return listToReturn

def runCommand (command):
    output=subprocess.run(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,shell=True)

    if output.returncode != 0:
        raise RuntimeError(
            output.stderr.decode("utf-8"))

    return output

username = getpass.getuser()
chd = f"/Users/{username}/Library/Java/JavaVirtualMachines" # add the absolute path where your JDKs are present here

os.chdir(chd) # Change to parent directory where all your JDKs are present to extract names of JDK in a list
# 
allInstalledExternalJavaJDKs = [name for name in os.listdir(".") if os.path.isdir(name)]
# print(allInstalledExternalJavaJDKs) # prints list of directories of all installed JDKs

outputList = printCommandsToLinkJDKWithSDKMan(allInstalledExternalJavaJDKs,chd) # send the list to method
print('Commands to run to link local JDKs: \n\n')
for cmd in outputList:
    print(cmd)

Output

Command Printed for all local JDKs to link to SDKman

Anmol Tomer
  • 189
  • 2
  • 7