How to create your first Quarkus application

The Quarkus framework is considered the rising star for Kubernetes-native Java.
68 readers like this.
woman on laptop sitting at the window

CC BY 3.0 US Mapbox Uncharted ERG

Programming languages and frameworks continuously evolve to help developers who want to develop and deploy applications with even faster speeds, better performance, and lower footprint. Engineers push themselves to develop the "next big thing" to satisfy developers' demands for faster deployments.

Quarkus is the latest addition to the Java world and considered the rising star for Kubernetes-native Java. It came into the picture in 2019 to optimize Java and commonly used open source frameworks for cloud-native environments. With the Quarkus framework, you can easily go serverless with Java. This article explains why this open source framework is grabbing lots of attention these days and how to create your first Quarkus app.

What is Quarkus?

Quarkus reimagines the Java stack to give the performance characteristics and developer experience needed to create efficient, high-speed applications. It is a container-first and cloud-native framework for writing Java apps.

You can use your existing skills to code in new ways with Quarkus. It also helps reduce the technical burden in moving to a Kubernetes-centric environment. High-density deployment platforms like Kubernetes need apps with a faster boot time and lower memory usage. Java is still a popular language for developing software but suffers from its focus on productivity at the cost of RAM and CPU.

In the world of virtualization, serverless, and cloud, many developers find Java is not the best fit for developing cloud-native apps. However, the introduction of Quarkus (also known as "Supersonic and Subatomic Java") helps to resolve these issues.

What are the benefits of Quarkus?

 

Quarkus improves start-up times, execution costs, and productivity. Its main objective is to reduce applications' startup time and memory footprint while providing "developer joy." It fulfills these objectives with native compilation and hot reload features.

Runtime benefits

 

  • Lowers memory footprint
  • Reduces RSS memory, using 10% of the memory needed for a traditional cloud-native stack
  • Offers very fast startup
  • Provides a container-first framework, as it is designed to run in a container + Kubernetes environment.
  • Focuses heavily on making things work in Kubernetes

Development benefits

 

  • Provides very fast, live reload during development and coding
  • Uses "best of breed" libraries and standards
  • Brings specifications and great support
  • Unifies and supports imperative and reactive (non-blocking) styles

Create a Quarkus application in 10 minutes

Now that you have an idea about why you may want to try Quarkus, I'll show you how to use it.

First, ensure you have the prerequisites for creating a Quarkus application

  • An IDE like Eclipse, IntelliJ IDEA, VS Code, or Vim
  • JDK 8 or 11+ installed with JAVA_HOME configured correctly
  • Apache Maven 3.6.2+

You can create a project with either a Maven command or by using code.quarkus.io.

Use a Maven command:

One of the easiest ways to create a new Quarkus project is to open a terminal and run the following commands, as outlined in the getting started guide

Linux and macOS users:

mvn io.quarkus:quarkus-maven-plugin:1.13.2.Final:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=getting-started \
    -DclassName="org.acme.getting.started.GreetingResource" \
    -Dpath="/hello"
cd getting-started

Windows users:

  • If you are using cmd, don't use the backward slash (\):
    mvn io.quarkus:quarkus-maven-plugin:1.13.2.Final:create -DprojectGroupId=org.acme -DprojectArtifactId=getting-started -DclassName="org.acme.getting.started.GreetingResource" -Dpath="/hello"
  • If you are using PowerShell, wrap -D parameters in double-quotes:

    mvn io.quarkus:quarkus-maven-plugin:1.13.2.Final:create "-DprojectGroupId=org.acme" "-DprojectArtifactId=getting-started" "-DclassName=org.acme.getting.started.GreetingResource" "-Dpath=/hello"

Use code.quarkus.io

First, go to code.quarkus.io to create a quick Quarkus project. This page will help you bootstrap your Quarkus application and discover its extension ecosystem.

Next, select any extension you need. Think of Quarkus extensions as project dependencies. Extensions help by configuring, booting, and integrating a framework or technology into your Quarkus app. They also do all the heavy lifting of providing the correct information to the GraalVM Java runtime for your app to compile natively.

Quarkus provides a growing list of over 50 best-of-breed libraries that developers love and use.

Expected results

Whichever way you create your Quarkus Project, it generates the following in ./getting-started:

  • The Maven structure
  • An org.acme.getting.started.GreetingResource resource exposed on /hello
  • An associated unit test
  • A landing page that is accessible on http://localhost:8080 after starting the app
  • Example Dockerfile files
  • The application configuration file

Project details

During project creation, it produces a src/main/java/org/acme/getting/started/GreetingResource.java file with the following content:

package org.acme.getting.started;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class GreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "Hello";
    }
}

It's a very simple REST endpoint that returns "Hello" to requests on the /hello endpoint.

Run the application

Now everything is set to run your application.

To do so, enter ./mvnw compile quarkus:dev:

 

Once it starts, you can request the provided endpoints:

 

Use Ctrl+C to stop the application, or keep it running and enjoy the blazing fast hot-reload.

Use development mode

To run Quarkus in development mode, enter quarkus:dev.

This command enables hot deployment. This means that whenever you change your Java or resource files and refresh the browser, you will automatically see all your changes. This also works for resource files, like the configuration property file. Refreshing the browser triggers a scan of the workspace; if it discovers any changes, it recompiles the Java files, redeploys the application, and services your request by the redeployed application. If there are any compilation issues, an Error 404 page will instantly give you feedback and help resolve the issue.

Package and run the application

To package the application, use ./mvnw package. It generates several outputs in /target:

  • One is the quarkus-app directory that contains the quarkus-run.jar file as an executable .jar. Be sure that it's not an "über-jar," as the dependencies are copied into subdirectories of quarkus-app/lib/.
  • The other is the getting-started-1.0.0-SNAPSHOT.jar, which contains only the classes and resources of your project. It is the regular artifact produced by the Maven build, and it's not the runnable .jar.

Run your app using:

java -jar target/quarkus-app/quarkus-run.jar

Yes, it is this simple, easy, and interesting.

Learn more

Quarkus offers several official resources where you can learn more about it:

What to read next
Saumya_Singh_RedHat
Engineer at Red Hat | Program Manager'20 @GirlScript | GHCI Scholar | International Open Source Award finalist by Red Hat | Winner SIH, 21U21 Award | Google Connect Winner'19 | Mentor GCI

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.