[Oracle Cloud] Create a development environment for OCI Java SDK (Visual Studio Code, Maven, CentOS)

Introduction

Here are the steps to prepare the development environment for the Java SDK of Oracle Cloud Infrastructure (OCI). Since I am building the environment while remembering each time, I will introduce it as a memorandum.

The development environment to prepare is as follows.

We will use Maven to enable the OCI SDK (Java) for Visual Studio Code.

Vistual Studio Code (Remote Development)

Install Java Extension Pack

1605448558140.png

OpenJDK 11

When working with Java in Visual Studio Code, you can use various functions with JDK 11 or higher.

sudo yum install -y java-11-openjdk java-11-openjdk-devel java-11-openjdk-src

Maven 3.6.3 Install

Install Maven to manage the dependencies.

Check the download URL at the following URL. https://maven.apache.org/download.cgi

1587385604751.png

Download the tar.gz file on CentOS using the URL you copied on the download page

mkdir ~/maven
cd ~/maven
wget https://ftp.jaist.ac.jp/pub/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz

Follow the steps below to install. https://maven.apache.org/install.html

Unzip the tar.gz file

tar xfvz apache-maven-3.6.3-bin.tar.gz

Environment variable settings Added to bashrc

echo 'export PATH=$PATH:$HOME/maven/apache-maven-3.6.3/bin' >> ~/.bashrc

bashrc reload

source ~/.bashrc

Make sure the mvn command is executable

[opc@maven maven]$ mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /home/opc/maven/apache-maven-3.6.3
Java version: 1.8.0_252, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.el7_8.x86_64/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "3.10.0-1127.8.2.el7.x86_64", arch: "amd64", family: "unix"
[opc@maven maven]$

Maven Directory

Create a working directory.

mkdir -p ~/java
cd ~/java

Created by Maven Project. Create it with the name ociproject.

mvn archetype:generate \
    -DarchetypeGroupId=org.apache.maven.archetypes \
    -DarchetypeArtifactId=maven-archetype-simple \
    -DarchetypeVersion=1.4 \
    -DgroupId=com.example \
    -DartifactId=ociproject \
    -Dversion=1.0-SNAPSHOT \
    -Dpackage=com.example

After creating Maven Project, edit pom.xml and set the dependency.

--Check and include the latest OCI SDK version, such as <version> 1.25.4 </ version>.

--Error if you do not enter com.sun.activation

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Refer to Maven Central Repository and add what you need https://search.maven.org/search?q=g:com.oracle.oci.sdk

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.oracle.oci.sdk</groupId>
        <artifactId>oci-java-sdk-bom</artifactId>
        <!-- replace the version below with your required version -->
        <version>1.25.4</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.oracle.oci.sdk</groupId>
      <artifactId>oci-java-sdk-audit</artifactId>
    </dependency>
    <dependency>
      <groupId>com.oracle.oci.sdk</groupId>
      <artifactId>oci-java-sdk-core</artifactId>
    </dependency>
    <dependency>
      <groupId>com.oracle.oci.sdk</groupId>
      <artifactId>oci-java-sdk-database</artifactId>
    </dependency>
    <dependency>
      <groupId>com.oracle.oci.sdk</groupId>
      <artifactId>oci-java-sdk-identity</artifactId>
    </dependency>
    <dependency>
      <groupId>com.sun.activation</groupId>
      <artifactId>javax.activation</artifactId>
      <version>1.2.0</version>
    </dependency>
  </dependencies>

Build Version adjustment (OCI SDK requires 8)

    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

Reference: Describe all of pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>ociproject</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>ociproject</name>
  <description>A simple ociproject.</description>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.oracle.oci.sdk</groupId>
        <artifactId>oci-java-sdk-bom</artifactId>
        <!-- replace the version below with your required version -->
        <version>1.25.4</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.oracle.oci.sdk</groupId>
      <artifactId>oci-java-sdk-audit</artifactId>
    </dependency>
    <dependency>
      <groupId>com.oracle.oci.sdk</groupId>
      <artifactId>oci-java-sdk-core</artifactId>
    </dependency>
    <dependency>
      <groupId>com.oracle.oci.sdk</groupId>
      <artifactId>oci-java-sdk-database</artifactId>
    </dependency>
    <dependency>
      <groupId>com.oracle.oci.sdk</groupId>
      <artifactId>oci-java-sdk-identity</artifactId>
    </dependency>
    <dependency>
      <groupId>com.sun.activation</groupId>
      <artifactId>javax.activation</artifactId>
      <version>1.2.0</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <reporting>
    <plugins>
      <plugin>
        <artifactId>maven-project-info-reports-plugin</artifactId>
      </plugin>
    </plugins>
  </reporting>
</project>

Downloads the libraries specified in dependencies. It will be downloaded to target / dependency in the Directory for Maven Project.

cd ~/java/ociproject
mvn dependency:copy-dependencies

Confirmation of downloaded jar files.

[opc@vault01 ociproject]$ ls -la target/dependency/
total 19252
drwxrwxr-x. 2 opc opc    4096 Nov 15 14:11 .
drwxrwxr-x. 3 opc opc      24 Nov 15 14:11 ..
-rw-rw-r--. 1 opc opc    3482 Nov 15 14:11 animal-sniffer-annotations-1.14.jar
-rw-rw-r--. 1 opc opc   14768 Nov 15 14:11 aopalliance-repackaged-2.5.0-b42.jar
-rw-rw-r--. 1 opc opc  796532 Nov 15 14:11 bcpkix-jdk15on-1.60.jar
-rw-rw-r--. 1 opc opc 4189874 Nov 15 14:11 bcprov-jdk15on-1.60.jar
-rw-rw-r--. 1 opc opc   31547 Nov 15 14:11 checker-compat-qual-2.0.0.jar
-rw-rw-r--. 1 opc opc  284184 Nov 15 14:11 commons-codec-1.10.jar
-rw-rw-r--. 1 opc opc  208700 Nov 15 14:11 commons-io-2.5.jar
-rw-rw-r--. 1 opc opc  434678 Nov 15 14:11 commons-lang3-3.4.jar
-rw-rw-r--. 1 opc opc   13704 Nov 15 14:11 error_prone_annotations-2.1.3.jar
-rw-rw-r--. 1 opc opc 2590643 Nov 15 14:11 guava-25.0-android.jar
-rw-rw-r--. 1 opc opc  186763 Nov 15 14:11 hk2-api-2.5.0-b42.jar
-rw-rw-r--. 1 opc opc  189454 Nov 15 14:11 hk2-locator-2.5.0-b42.jar
-rw-rw-r--. 1 opc opc  135317 Nov 15 14:11 hk2-utils-2.5.0-b42.jar
-rw-rw-r--. 1 opc opc    8782 Nov 15 14:11 j2objc-annotations-1.1.jar
-rw-rw-r--. 1 opc opc   66894 Nov 15 14:11 jackson-annotations-2.9.8.jar
-rw-rw-r--. 1 opc opc  325619 Nov 15 14:11 jackson-core-2.9.8.jar
-rw-rw-r--. 1 opc opc 1347236 Nov 15 14:11 jackson-databind-2.9.8.jar
-rw-rw-r--. 1 opc opc   33391 Nov 15 14:11 jackson-datatype-jdk8-2.9.8.jar
-rw-rw-r--. 1 opc opc  100674 Nov 15 14:11 jackson-datatype-jsr310-2.9.8.jar
-rw-rw-r--. 1 opc opc   34610 Nov 15 14:11 jackson-module-jaxb-annotations-2.8.10.jar
-rw-rw-r--. 1 opc opc  737884 Nov 15 14:11 javassist-3.22.0-CR2.jar
-rw-rw-r--. 1 opc opc   26366 Nov 15 14:11 javax.annotation-api-1.2.jar
-rw-rw-r--. 1 opc opc    2497 Nov 15 14:11 javax.inject-1.jar
-rw-rw-r--. 1 opc opc    5951 Nov 15 14:11 javax.inject-2.5.0-b42.jar
-rw-rw-r--. 1 opc opc  127509 Nov 15 14:11 javax.ws.rs-api-2.1.jar
-rw-rw-r--. 1 opc opc    2254 Nov 15 14:11 jcip-annotations-1.0.jar
-rw-rw-r--. 1 opc opc  181563 Nov 15 14:11 jersey-client-2.27.jar
-rw-rw-r--. 1 opc opc 1140395 Nov 15 14:11 jersey-common-2.27.jar
-rw-rw-r--. 1 opc opc   69758 Nov 15 14:11 jersey-entity-filtering-2.27.jar
-rw-rw-r--. 1 opc opc   62547 Nov 15 14:11 jersey-hk2-2.27.jar
-rw-rw-r--. 1 opc opc   73055 Nov 15 14:11 jersey-media-json-jackson-2.27.jar
-rw-rw-r--. 1 opc opc   77882 Nov 15 14:11 json-smart-1.3.1.jar
-rw-rw-r--. 1 opc opc   19936 Nov 15 14:11 jsr305-3.0.2.jar
-rw-rw-r--. 1 opc opc  250436 Nov 15 14:11 nimbus-jose-jwt-4.9.jar
-rw-rw-r--. 1 opc opc   75337 Nov 15 14:11 oci-java-sdk-audit-1.5.2.jar
-rw-rw-r--. 1 opc opc  247871 Nov 15 14:11 oci-java-sdk-common-1.5.2.jar
-rw-rw-r--. 1 opc opc 4010670 Nov 15 14:11 oci-java-sdk-core-1.5.2.jar
-rw-rw-r--. 1 opc opc 1380390 Nov 15 14:11 oci-java-sdk-database-1.5.2.jar
-rw-rw-r--. 1 opc opc   20235 Nov 15 14:11 osgi-resource-locator-1.0.1.jar
-rw-rw-r--. 1 opc opc   41203 Nov 15 14:11 slf4j-api-1.7.25.jar
-rw-rw-r--. 1 opc opc   63777 Nov 15 14:11 validation-api-1.1.0.Final.jar

Goto Definition of source code is possible from Visual Studio Code by mvn install.

mvn install

Settings related to log4j

The OCI SDK uses log4j. If you do not set log4j, Warning will continue to appear. This time, create it under the src directory.

mkdir $HOME/java/ociproject/src/main/resources

Create log4j.properties

cat <<'EOF' >$HOME/java/ociproject/src/main/resources/log4j.properties
log4j.rootLogger=ERROR,stdout
log4j.logger.com.endeca=INFO
# Logger for crawl metrics
log4j.logger.com.endeca.eidi.web.metrics=INFO

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n
EOF

Sample Code

The development environment is ready up to this point. Let's execute Sample Code to check the operation of the SDK. Sample Code that displays a list of Compute Instances and prints to standard output.

I don't want to have a private key for authentication, so I use Instance Principle. https://qiita.com/sugimount/items/159852749851282a33e6

Sample Code

package com.example;

import com.oracle.bmc.auth.InstancePrincipalsAuthenticationDetailsProvider;
import com.oracle.bmc.core.ComputeClient;
import com.oracle.bmc.core.requests.ListInstancesRequest;
import com.oracle.bmc.core.responses.ListInstancesResponse;

/**
 * Hello world!
 *
 */
public class App {
    public static void main(String[] args) {
        System.out.println("Hello World!");

        InstancePrincipalsAuthenticationDetailsProvider provider = InstancePrincipalsAuthenticationDetailsProvider
                .builder().build();
        ComputeClient computeClient = new ComputeClient(provider);

        // ListInstancesRequest.builder().build();

        ListInstancesRequest listInstancesRequest = ListInstancesRequest.builder()
                .compartmentId("ocid1.tenancy.oc1..aaaaaaaahysodjbc46bsbxjwtu4hcxos6uxhhldgeeq5yvw4vrwsdz34sfta")
                .build();

        ListInstancesResponse response = computeClient.listInstances(listInstancesRequest);
        // System.out.println(response);

        computeClient.close();
    }
}

Execution example

[opc@java01 ociproject]$  cd /home/opc/java/ociproject ; /usr/lib/jvm/java-11-openjdk-11.0.9.11-0.0.1.el7_9.x86_64/bin/java -Dfile.encoding=UTF-8 @/tmp/cp_5zz9fok3mfupnsg33snsk49h.argfile com.example.App 
Start!
bastion_onpremis
java01
private_machine01
vault01
vyos
End!

Reference URL

https://docs.cloud.oracle.com/ja-jp/iaas/Content/API/SDKDocs/javasdkgettingstarted.htm

Recommended Posts

[Oracle Cloud] Create a development environment for OCI Java SDK (Visual Studio Code, Maven, CentOS)
I tried "Visual Studio Code Installer for Java" which can create Java development environment immediately
A record of setting up a Java development environment with Visual Studio Code
Build Java program development environment with Visual Studio Code
Build WebAPP development environment with Java + Spring with Visual Studio Code
Oracle Cloud [OCI]: Create a CentOS Stream instance using Compute's cloud-init
Let's create a Java development environment (updating)
Create a java web application development environment with docker for mac part2
Build a development environment for Docker, java, vscode
Create a docker environment for Oracle 11g XE
Build a Java development environment with VS Code
Create a Java development environment using jenv on Mac
Let's create a gcloud development environment on a centos8 container
Create a Java, JavaScript team development environment (problem raising)
[Environment construction] Build a Java development environment with VS Code!
I tried to create a java8 development environment with Chocolatey
Java in Visual Studio Code
Create a Java and JavaScript team development environment (gradle environment construction)
Spring5 MVC Web application development with Visual Studio Code Environment construction (Installation of JDK11, Maven, Tomcat, Visual Studio Code)
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
Build Java development environment (for Mac)
Java development environment (Mac, VS Code)
Let's install Docker on Windows 10 and create a verification environment for CentOS 8!
Building a development environment for Flutter on Win10 --- Flutter SDK Install (2020 preservation version)
Spring5 MVC Web application development with Visual Studio Code Maven template creation
Sample code collection for Azure Java development
Build Java 8 development environment on AWS Cloud9
[Mac] Install Java in Visual Studio Code
Build a development environment for Docker + Rails6 + Postgresql
[Memo] Create a CentOS 8 environment easily with Docker
Add --enable-preview option in Java in Visual Studio Code
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
[Beginner] Install java development tool in cloud9 development environment.
[Mac] Install Java in Visual Studio Code (VS Code)
Create a Spring Boot development environment with docker
[For beginners] Until building a Web application development environment using Java on Mac OS
[SAP] Create a development environment with NW AS ABAP Developer Edition (2) Connect with Cloud Platform
Create a Java (Maven) project with VS Code and develop it on a Docker container