Skip to main content

Development and debugging with Java

Introduction

Basic knowledge in Java is required before you start this tutorial.

In this tutorial we will cover:

In this example we will use the IntelliJ IDEA Community Edition IDE and Java JDK 1.8.

Initial Configuration

The first step will be to create a new project in _IntelliJ IDEA_. To do so, follow the steps below:

First Step

Choose the "Create new project" option.

desenvolvimento-debug-java1.png

Second Step

Choose Java from the left menu [1] and later configure the version of Java JDK [2], if you haven't done it initially, click on "New" and indicate its installation directory.

desenvolvimento-debug-java2.png

Third Step

Click in " Next" until a screen similar to the following shows and then define the name of the project, indicate the Netuno directory and click in "Finish"

desenvolvimento-debug-java3.png

Fourth Step

Create a new classes folder within the directory

netuno/web/WEB-INF/

Right click on the project folder and choose "Open module settings" [1].

desenvolvimento-debug-java4.png

Then click on the "Paths" tab, choose the "Use module compile output path" [2] and indicate in both the netuno/web/WEB-INF/classes/ directory [3].

desenvolvimento-debug-java5.png

Finally go to the "Dependencies" tab and click on the "+" icon, choose "JARs or directories" and indicate the file netuno.jar [4] and then the directory netuno/web/WEB-INF/lib [5]

desenvolvimento-debug-java6.png

Fifth Step

Configure the local server to run Netuno, start by choosing "Application". Insert the main class org.netuno.cli.Main[1], insert as argument server[2] and indicate the project directory [3], finally just click on "Aplly" and "Ok"

desenvolvimento-debug-java7.png desenvolvimento-debug-java8.png

desenvolvimento-debug-java9.png

Building a Simple Service

When expanding the project folder, access the src folder, create a new package, and then create a class.

In this example we have called the package org.netuno.tritao and the class Test as shown in the following image:

desenvolvimento-debug-java10.png

Finally we add the following code that causes the data to be output from the service in json format.

package org.netuno.tritao;

import org.netuno.proteu.Proteu;
import org.netuno.proteu.ProteuException;
import org.netuno.psamata.Values;
import org.netuno.tritao.config.Hili;

import java.io.IOException;

public class Test {

public static void _main(Proteu proteu, Hili hili)
throws IOException, ProteuException {
proteu.outputJSON(
new Values()
.set("message", "Hello World")
.set("description", "Debugging from Java...")
.set("id", 1)
.set("result", true)
);
}

}

Note that depending on your web browser and the input data the result may differ from this example

Running the server in normal mode

Run the local server through the triangle shaped icon [1] and if it is well configured the tab called "Run" will expand resembling the second image:

desenvolvimento-debug-java11.png

desenvolvimento-debug-java12.png

Accessing the service

To access the service created previously, login to Netuno and access the service, in this example the link will be http://localhost:9000/Test.netuno.

A screen like the following will show if everything is set correctly:

desenvolvimento-debug-java13.png

Running the server in debug mode

To use this mode, start the server through the icon that resembles an insect [1] and put some breakpoints in the code to see their values [2] as represented in the following images:

desenvolvimento-debug-java14.png

desenvolvimento-debug-java15.png

If you have any questions, please do not hesitate to contact us.

All the best!