Setup Java Project

Now that our development environment is setup, let’s create a new Gradle project so we can begin coding.

First, create a new directory called ‘lab2’ for our project and change into it.

mkdir lab2
cd lab2

Now initialize Gradle for our project.

gradle init --type java-application

When prompted, select Groovy for the build script DSL and JUnit 4 for the test framework. Press Enter to keep the default values for project name and package.

Initialize Gradle

In the left-hand navigation, click the arrow next to the ‘lab2’ folder to expand it. In this folder, you will see the build.gradle file. Open the build.gradle file in the IDE by double-clicking on it.

build.gradle build.gradle

Add the following lines to the dependencies section of the file to incorporate the third-party libraries we’ll need for the lab.

compile group: 'com.amazon.ion', name: 'ion-java', version: '1.6.1'
compile group: 'software.amazon.qldb', name: 'amazon-qldb-driver-java', version: '2.0.0-rc.1'
compile group: 'com.amazonaws', name: 'aws-java-sdk-qldb', version: '1.11.785'

The dependencies section of the file should now look like the following:

dependencies {
    // This dependency is used by the application.
    implementation 'com.google.guava:guava:29.0-jre'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.13'

    compile group: 'com.amazon.ion', name: 'ion-java', version: '1.6.1'
    compile group: 'software.amazon.qldb', name: 'amazon-qldb-driver-java', version: '2.0.0-rc.1'
    compile group: 'com.amazonaws', name: 'aws-java-sdk-qldb', version: '1.11.785'
}

Add the following line to the repositories section of the build.gradle file.

mavenCentral()

The repositories section of the file should now look like the following:

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    mavenCentral()
}

Save the build.gradle file by clicking “File” on the menu bar and then clicking “Save”.

App.java

Open the App.java file in the IDE by double-clicking on it in the left-hand navigation. The file is located in /qldb-lab2/lab2/src/main/java/lab2 in the project tree or /home/ec2-user/environment/lab2/src/main/java/lab2 on the filesystem.

App.java

Replace the contents of the file with the code below.

package lab2;

public class App {

    public static void main(String[] args) {
        System.out.println("Let's play with Ion!");
    }
}

Save the file.

Gradle creates a unit test during initialization. We won’t be working with the unit test in this lab, so let’s remove it to keep it from failing during builds as we work with the App.java file.

Find the /qldb-lab2/lab2/src/test/java/lab2/AppTest.java file in the left-hand navigation in the IDE. Right-click the AppTest.java file and select ‘Delete’. Click ‘Yes’ to confirm deletion.

Make sure that your terminal session is in the same directory as the build.gradle file.

pwd

Make sure that all of your files have been saved before proceeding. Click “File” and “Save All”.

pwd

Test the program by running the command below in Cloud9’s terminal window.

gradle run

You should see the output depicted below. Test Output

Now that we know our environment is setup correctly and we can compile and execute Java programs, let’s begin using Amazon Ion.