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)

