Skip to main content
Version: 1.26

Getting Started with Okteto and Java

Okteto is a platform that simplifies the process of launching cloud development environments without requiring the expertise to do this yourself. This enables developers to automatically spin up fully-managed development environments that emulate production as closely as possible. Okteto eliminates the friction of local development environments, the many deviations that can exist for the same engineering organization, and the troubleshooting that comes with them.

This tutorial will show you how to develop and debug a Java application using Okteto.

Prerequisites

Install the latest version of the Okteto CLI and configure it to access Okteto. Follow our installation guide if you haven't done it yet.

Step 1: Deploy the Java Sample Maven or Gradle App

Get a local version of the Java Sample App by executing the following commands:

Maven

$ git clone https://github.com/okteto/java-maven-getting-started
$ cd java-maven-getting-started

Gradle

$ git clone https://github.com/okteto/java-gradle-getting-started
$ cd java-gradle-getting-started

At the root of the directory, you'll find the okteto.yml file. This file describes how to deploy, and develop the Java Sample App.

Maven

okteto.yml
deploy:
- kubectl apply -f k8s.yml

Gradle

okteto.yml
deploy:
- kubectl apply -f k8s.yml

Deploy your development environment by executing:

$ okteto deploy

The output should look like this:

Maven

 i  Using cindy @ okteto.example.com as context
i Running kubectl apply -f k8s.yml
deployment.apps/hello-world created
service/hello-world created
ingress.networking.k8s.io/hello-world created
✓ Development environment 'java-maven-getting-started' successfully deployed
i Run 'okteto up' to activate your development container

Gradle

 i  Using cindy @ okteto.example.com as context
i Running kubectl apply -f k8s.yml
deployment.apps/hello-world created
service/hello-world created
ingress.networking.k8s.io/hello-world created
✓ Development environment 'java-gradle-getting-started' successfully deployed
i Run 'okteto up' to activate your development container

Log into your Okteto instance and click on the endpoint URL for the application:

Okteto UI Java

Notice that you're accessing your application through an HTTPS endpoint? This is because Okteto will automatically create them for you when you deploy your application. Cool, no 😎?

Step 2: Activate your development container

The dev section defines how to activate a development container for the Java Sample App.

Maven

okteto.yml
dev:
hello-world:
image: okteto/maven:3
command: bash
sync:
- .:/usr/src/app
forward:
- 5005:5005
volumes:
- /root/.m2

Gradle

okteto.yml
dev:
hello-world:
image: okteto/gradle:6.5
command: bash
sync:
- .:/usr/src/app
forward:
- 5005:5005
volumes:
- /home/gradle/.gradle

The hello-world key matches the name of the hello world Deployment. The other fields represent:

  • image: the image used by the development container.
  • command: the start command of the development container.
  • sync: the folders synchronized between your local machine and the development container.
  • forward: a list of ports to forward from your development container. This is needed to configure the Java remote debugger.
  • volumes: a list of paths in your development container to be mounted as persistent volumes. This is useful to persist the maven/gradle caches.

In the project root, there is an .stignore file to indicate which files shouldn't be synchronized to your development container. This is useful to avoid synchronizing binaries, build artifacts, or git metadata.

Step 3: Activate your development container

Next, execute the following command to activate your development container:

$ okteto up
 ✓  Persistent volume successfully attached
✓ Images successfully pulled
✓ Files synchronized
Namespace: cindy
Name: hello-world
Forward: 5005 -> 5005

Welcome to your development container. Happy coding!
cindy:hello-world app>

Working in your development container is the same as working on your local machine. Start the application by running the following command:

Maven

app>
mvn spring-boot:run

Gradle

app>
app> gradle bootRun

The first time you run the application, Maven/Gradle will compile your application. Wait for this process to finish.

Go back to the browser and reload the page to test that your application is running.

Step 4: Remote Development with Okteto

Open src/main/java/com/okteto/helloworld/RestHelloWorld.java in your favorite local IDE and modify the response message on line 11 to be Hello world from Okteto!. Save your changes.

package com.okteto.helloworld;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestHelloWorld {

@GetMapping("/")
public String sayHello() {
return "Hello world from Okteto!";
}
}

Your IDE will auto compile only the necessary *.class files which will be synchronized by Okteto to your remote environment. Take a look at the development container shell and notice how the changes are detected by Spring Boot and automatically hot reloaded.

tip

The spring-boot-devtools dependency is already included to automatically restart your Java application whenever a file is changed.

When using your own spring application don't forget to include spring-boot-devtools to enable hot reloading.

Go back to the browser and reload the page. Your code changes were instantly applied. No commit, build, or push required 😎!

Step 5: Remote debugging with Okteto

Okteto enables you to debug your applications directly from your favorite IDE. Let's take a look at how that works in IntelliJ, one of the most popular IDEs for Java development.

Add the following JVM arguments in the Gradle/Maven configuration files to enable remote debugging in your Java application:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

Create the debug configuration on IntelliJ

  1. From the main menu, select Run | Edit Configurations.
  2. In the Run/Debug Configuration dialog, click + on the toolbar or press ⌘ N. The list shows the run/debug configuration templates. Select Remote JVM Debug.
  3. Specify the run/debug configuration name in the Name field. This name will be shown in the list of the available run/debug configurations.
  4. In the Configuration section, specify the parameters listed below:
    • Debugger Mode: Attach to remote JVM
    • Host: localhost
    • Port: 5005 (this should match the port forward on your Okteto manifest file)
    • Command line arguments for remote JVM: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
  5. Apply the changes and close the dialog.
debugging in Okteto with Java

Start the remote debugging session

Select the debug action you just added, and click on the debug button to start the remote debugging session.

Add a breakpoint on src/main/java/es/okteto/helloworld/RestHelloWorld.java, line 11. Go back to the browser and reload the page. The execution will halt at your breakpoint. You can then inspect the request, the available variables, etc...

breakpoint in Java

Your code is executing in your remote development environment, but you can debug it from your local machine without any extra services or tools!

Next steps

Congratulations, you just developed your first application in Okteto 🚀.

Okteto lets you develop your applications directly on Kubernetes. This way you can:

  • Eliminate integration issues by developing in a realistic environment
  • Test your application end to end as fast as you type code
  • Avoid wasting CPU cycles on your machine and develop at the speed of the cloud!

Find more advanced samples with Okteto in this repository or join our community to ask questions and share your feedback.