How to use local jdk in SDKMAN! script

1.2k views Asked by At

My script changes to a jdk installed with sdkman but not to a local one. How can I change into my local oracle 8 v261 jdk in a script with sdkman?

The script

#!/bin/bash
. /usr/local/sdkman/bin/sdkman-init.sh
sdk ls java
for i in {"8_261-oracle", "9.0.4-open"}
do
  sdk u java $i
done

gives as output

[...]
               |     | 9.0.4        | open    | installed  | 9.0.4-open
               | >>> | 8.0.265      | open    | installed  | 8.0.265-open
               |     | 8.0.232      | open    | local only | 8.0.232-open
[...]
 Unclassified  |     | 8_261        | none    | local only | 8_261-oracle
================================================================================
Use the Identifier for installation:

    $ sdk install java 11.0.3.hs-adpt
================================================================================

Stop! java 8_261-oracle, is not installed.

Using java version 9.0.4-open in this shell.

I was inspired by How to use SDKMAN! to install packages from within scripts.

1

There are 1 answers

0
Alessandro S. On

You could parse the output of sdkman to retrieve the list of installed sdks as follows:

#!/bin/bash

sdks=`sdk list java | grep installed | awk -F"|" '{print $6}'`

for sdk in ${sdks[@]}; do
  sdk use java $sdk
  ### YOUR CODE HERE
done

I have omitted the . /usr/local/sdkman/bin/sdkman-init.sh part because I suggest to set it up in bashrc/zshrc as suggested in sdkman doc.

An example from my ~/.zshrc:

#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="$YOUR_PATH/.sdkman"
[[ -s "$YOUR_PATH/.sdkman/bin/sdkman-init.sh" ]] && source "$YOUR_PATH/.sdkman/bin/sdkman-init.sh"