Saturday, 25 July 2020

Another cup of Java, Joe

For my most recent project, I'm co-developing a plugin for Jenkins which required me to dust off my Java skills - it's been a while since I was coding Enterprise Java Beans, servlets, portlets and JavaServer Pages etc.

For the record, my first job in Hursley in 1997 required me to have some knowledge of Java - I went to a book store ( remember them ? ) and bought a book on .... JavaScript.

Surely they're the same, I thought ....... Can you say "Doh" ?

Anyhow, this most recent challenge requires me to code against the Jenkins and Docker APIs using Java 8.

On my Mac, I have multiple versions of Java installed, including Java 8 itself, as per System Preferences -> Java : -



However, I'm also using Microsoft Visual Studio as my Integrated Development Environment (IDE), which, as per this, requires Java 11.

Therefore, I have three Java versions installed.

One nice thing - macOS has a neat way to manage the installed Java Runtime Environments (JRE) and Java Development Kits (JDK) : -

/usr/libexec/java_home -V

Matching Java Virtual Machines (3):
    11.0.8, x86_64: "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
    1.8.0_262, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
    1.8.0_251, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home


And one can choose the version thusly: -

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_251`; java -version

java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)


export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_262`; java -version

openjdk version "1.8.0_262"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.262-b10, mixed mode)


export JAVA_HOME=`/usr/libexec/java_home -v 11.0.8`; java -version

openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)


which allows me to set the requisite version in ~/.bash_profile.

vi ~/.bash_profile

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_251`; java -version

source ~/.bash_profile

java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)


The upside of having java -version added to the profile is that the Java version is reported whenever I open Terminal: -



No comments:

Note to self - use kubectl to query images in a pod or deployment

In both cases, we use JSON ... For a deployment, we can do this: - kubectl get deployment foobar --namespace snafu --output jsonpath="{...