Installing Java with sdkman
It has been a while since I have done something with Java, so I thought of getting up to speed and trying out languages that run on the JVM platform, such as Java (1995), Groovy (2003), and Kotlin (2011), as well as some build tools like Maven (2004) and Gradle (2008).
This tutorial aims to demonstrate how easy it is to install a Java JDK and other popular SDKs used in the Java ecosystem using SDKMan.
To illustrate the setup and use of these frameworks, I created a few demo projects. These projects are simple “hello-world” web application servers built using the web microframework SparkJava (2011), which utilizes Jetty (1995). The demos include:
- Java using Maven build tools
- Groovy using Gradle build tools with Groovy DSL
- Kotlin using Gradle build tools with Kotlin DSL
Requirements
Tool Requirements
- POSIX Shell [
sh
] such as bash [bash
] or zsh [zsh
] are used to run the commands. These come standard on most Linux distros.
For macOS, the versions of bash [bash
] or zsh [zsh
] are older versions, but should work. If you installed a package manager like MacPorts or Homebrew, you can update these shells to a more current version, e.g. brew install bash
.
For Windows, there is no POSIX shell available, so you’ll need to install tool like MSYS2 or Git-Bash to get a POSIX shell like bash
. If you have a package manager like Chocolatey, you can install any of those environments, e.g. choco install msys2
.
Knowledge Requirements
These are the areas that are helpful.
- Basic command line shell usage: environment variables, shell pipes, shell redirection, process management, etc.
- Compiler and Linker concepts are useful to understanding what tasks that tools like Maven and Gradle are doing.
- Overview of the the Java Platform: JVM (Java Virtual Machine) and byte-code vs machine code.
Installation
Installing SDK Man
Installation of this version manager is straightforward.
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
This installation will modify your shell startup scripts. You should adapt or modifies these additions as appropriate.
Installing Java JDK
Unlike early days of Java, where was one or two JDKs, there are now many JDK options to choose from. SDKMan has a list of JDKS that are supported.
My colleagues recommend Temurin builds. JetBrains uses Amazon Corretto for Windows and for macOS on Apple Silicon (ARM64), they use Azul Zulu builds of OpenJDK.
Here’s an example of installing a Azul Zulu version that is compatible with the current version of Gradle:
JAVA_JDK_VERSION="21.0.3-zulu"
sdk install java $JAVA_JDK_VERSION
Installing Languages and Tools
Here’s an easy way to install recent versions of Kotlin, Groovy, Maven, and Gradle.
latest_version() {
sdk list $1 \
| grep -vE '.*-.*' \
| grep -oE '\b[0-9]+\.[0-9]+\.[0-9]+\b' \
| sort \
| tail -1
}
# find latest versions
GROOVY_VERSION=$(latest_version groovy)
KOTLIN_VERSION=$(latest_version kotlin)
MAVEN_VERSION=$(latest_version maven)
GRADLE_VERSION=$(latest_version gradle)
# install tools and languages
sdk install groovy $GROOVY_VERSION
sdk install kotlin $KOTLIN_VERSION
sdk install maven $MAVEN_VERSION
sdk install gradle $GRADLE_VERSION
Demo Applications with SparkJava
Below are some example applications using tools and languages installed earlier.
Demo: Java and Maven
Several years ago, I developed a small hello-world project called webmf-java-spark using SparkJava, a lightweight web micro-framework similar to Sinatra (ruby
) or Flask (python
).
The project employs the Apache Maven build system, which, impressively, has continued to work reliably over the years. You can clone the project and run it with the following instructions below:
git clone git@github.com:darkn3rd/webmf-java-spark.git
cd webmf-java-spark
Typically, for small projects like this, I would prefer to have the code and instructions all within this tutorial. However, the build script is in XML, which is difficult to manipulate from the command line. Generally, working with XML from the command line isn’t very common.
Below is the code from the repository to give you an example of SparkJava:
package com.mycompany.app;
import static spark.Spark.*;
public class App {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hello World!\n");
get("/", (req, res) -> "Hello World!\n");
get("/hello/", (req, res) -> "Hello World!\n");
get("/hello/:name", (request, response) -> {
return "Why, Hello " + request.params(":name") + "!\n";
});
}
}
To compile the code with maven, you can run this:
# compile and package the application
mvn compile
mvn package
# run the server
mvn exec:java -Dexec.mainClass="com.mycompany.app.App"
In another terminal tab, run these commands to test out the server.
curl http://127.0.0.1:4567/
curl http://127.0.0.1:4567/hello
curl http://127.0.0.1:4567/hello/Michihito
Demo: Groovy and Gradle
Groovy emerged as a favored language for automation within the Java ecosystem. Initially embraced as a scripting language for the JVM, its popularity spread to various domains, including build-task platform Gradle, automation in Jenkins CI with the pipelines-as-code plugin, code within the Spinnaker CD platform, the Spock test framework, and the Grails web framework.
Inspired by this versatility, I crafted a hello-world project called webmf-groovy-spark, using the same SparkJava web micro-framework from the previous, but now this time using Gradle build tools. Unfortunately, the auto-generated build scripts from 2016 no longer work, so updated the project to support the current version of the tools with Gradle 8.
You can get the webmf-groovy-spark repository using:
git clone git@github.com:darkn3rd/webmf-groovy-spark.git
cd webmf-java-spark.git
The code is Groovy looks like the following below:
package com.mycompany.app
import static spark.Spark.*
get('/', { req, res -> "Hello World!\n" })
get('/hello', { req, res -> "Hello World!\n" })
get('/hello/', { req, res -> "Hello World!\n" })
get("/hello/:name", { req, res -> "Why, Hello ${req.params(":name")}!\n" })
To run the server, you can run the following:
./gradlew run
In another terminal tab, run these commands to test the server.
curl http://127.0.0.1:4567/
curl http://127.0.0.1:4567/hello
curl http://127.0.0.1:4567/hello/Yukie
As this comes with a test framework, you can run the tests with:
./gradlew test
Demo: Kotlin and Gradle
Kotlin is becoming extremely popular as a development language and scripting language on the Java platform. So I decided to try out this solution, and created a similar project published as webmf-kotlin-spark, which you can get using:
git clone git@github.com:darkn3rd/webmf-groovy-spark.git
cd webmf-java-spark.git
The code for this looks like:
package com.mycompany.app
import spark.kotlin.*
fun initializeServer() {
port(4567) // Set the port before defining routes
get("/hello") { "Hello World!\n" }
get("/") { "Hello World!\n" }
get("/hello/") { "Hello World!\n" }
get("/hello/:name") { "Why, Hello " + params(":name") + "!\n" }
}
fun main() {
initializeServer()
}
To run the server, you can run the following:
./gradlew run
In another terminal tab, run these commands to test the server.
curl http://127.0.0.1:4567/
curl http://127.0.0.1:4567/hello
curl http://127.0.0.1:4567/hello/Kumiko
As this comes with a test framework, you can run the tests with:
./gradlew test
Concusion
As part of demonstrating these languages and tools, this tutorial In this tutorial, we focused on using SDKMan as the primary tool for installing JDKs and various SDKs essential for the Java ecosystem. SDKMan simplifies the installation and management of multiple versions of SDKs, making it a powerful tool for developers working with Java and JVM languages.
By following the provided steps, you should now be able to:
- Install Java JDK and other essential SDKs effortlessly using SDKMan.
- Manage and switch between different versions of Java, Groovy, Kotlin, Maven, and Gradle with ease.
To illustrate the practical application of these installations, we created simple “hello-world” web application servers using SparkJava. These demos highlighted how to:
- Use Maven to build and run a Java project.
- Use Gradle to build and run a Groovy project.
- Use Gradle to build and run a Kotlin project.
The code repositories for the above may include unit tests available using either Spock or kotlin.test should this be of interest.
These examples demonstrate the convenience and power of SDKMan in setting up your development environment, allowing you to focus on coding rather than configuring your tools. Whether you’re trying out Java or exploring new JVM languages, SDKMan provides a streamlined and efficient way to manage your development tools.
Resources
Spark web-microframework
- Spark Micro Framework by Vashitva Srivastava on June 21, 2016
- Spark source code
- Spark-Kotlin source code
- SparkJava home page