Skip to Content
Technical Articles

Tutorial for beginners: Building Java Application with MongoDB on SAP CP

Hi All,

I am writing this blog to describe some easy steps to build Java application with MongoDB on SAP Cloud Platform Cloud Foundry environment.

I just followed the official tutorial, since I am a beginner.  I added some comments for avoiding for mistakes.

Environment

Local PC

  • Ubuntu18.04.01 LTS on VMWare Workstation
  • cf CLI 6.43.0

Cloud Foundry

  • Java Buildpack version 4.17.2
  • CF Trial (Europe – Frankfurt)

Prerequisites

  • your space is created on Cloud Foundry environment
  • cf CLI is installed on Local PC(see the official page for the installation)
  • git is installed on Local PC

Steps

1. Install JDK and maven

At first, I tried to use OracleJDK, so uninstalled OpenJDK.  Though It is also OK that OracleJDK and Open JDK coexists on PC, I chose the way to install only OpenJDK.

1.1. Uninstalled current openJDK

# Check installed Java version
java -version

# chcek package name
sudo dpkg --list | grep -i jdk

# uninstall packages
sudo apt remove openjdk-8-jre-headless openjdk-8-jre

1.2. Install openJDK-8

Then I tried to install OracleJDK, but I found that to register my account was troublesome.  So I installed OpenJDK.

sudo apt search openjdk-\(\.\)\+-jdk$
sudo apt-get install openjdk-8-jdk

somehow openjdk-11-jre-headless was installed, so I uninstalled the one.

sudo apt remove openjdk-11-jre-headless

Checked Java version.

$ java -version

openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

1.3. Install Maven

sudo apt install maven

checked installed Maven version

$ mvn -v
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-47-generic", arch: "amd64", family: "unix"

1.4. update .bashrc

vim ~/.bashrc

I added following two lines at the end of .bashrc

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH="$PATH:$JAVA_HOME/bin:/usr/share/maven/bin"

After restarting terminal, checked JAVA_HOME and PATH

echo $JAVA_HOME
echo $PATH

 

2. Create mongoDB and Java application

2.1. Create mongoDB service

# input user and password after "cf login"
cf login

# check mongo db service plan name
cf marketplace -s mongodb

# create mongoDB service
cf create-service mongodb v3.0-dev mongo-service

2.2.  Create the Java maven project first

mvn archetype:generate -DgroupId=hcp-cf-mongodb-tutorial -DartifactId=hcp-cf-mongodb-tutorial   -DinteractiveMode=false

2.3. Clone tutorial repository

# change directory to somewhere
cd <directory>
git clone https://github.com/SAP/hcp-cloud-foundry-tutorials.git

2.4. Build the tutorial project

cd hcp-cloud-foundry-tutorials/hcp-cf-mongodb-tutorial
mvn package

2.5. Change manifest.yml

Since I failed with downloaded “manifest.yml”, I changed the file a little bit.  The failure was shortage of memory.

---
applications:
- name: hcp-cf-mongodb-tutorial
  memory: 768M
  path: target/hcp-cf-mongodb-tutorial-1.0-SNAPSHOT.jar
  services:
  - mongo-service

2.6. Push the project

cf push

 

Result

After successful pushing, I accessed the Java app via browser.

I can see a message on browser!

{"status":"Successfully accessed Mongodb service. Retrieving the data object inserted in test collection.","dbObjects":[{"key":"hello","value":"world"}]}
1 Comment
You must be Logged on to comment or reply to a post.
  • Hi Yohei,

    Thank you very much for the tutorial, it is very informative. I have a general question related to mongoDB service. I would like to use a Raspberry PI to send data directly to mongoDB. service Do you know this is possible?

    thanks,