Search

Getting started with Java

Overview

The CAP Java SDK enables developing CAP applications in Java. While the SAP Web IDE provides excellent support to develop CAP Java applications, you can also develop locally with your tool of choice, for example Eclipse.

The CAP Java SDK supports lean application design by its modular architecture, that means you pick the required features and add them to your application dependencies on demand.

It enables local development by supporting in-memory or file-based SQLite databases. At the same time the CAP Java SDK enables switching to a productive environment, using, for example, SAP HANA as a database, easily by simply switching the application deployment configuration.

If you use Spring Boot, you find yourself directly at home when using the CAP Java SDK, as the framework integrates nicely with Spring Boot features like transaction handling, auto-wiring and test support. While the CAP Java SDK is framework agnostic, it’s also possible to develop plain Java applications or even integrate with other frameworks.

The CAP Java SDK comes with an OData V4 protocol adapter, but it’s openly designed. You can add more protocol adapters in the future or provide even custom protocol adapters by the application.

It supports SAP Cloud Platform features like authentication and authorization based on XSUAA tokens. But you are not locked in to SAP Cloud Platform using a CAP Java applications.

Excited? The following sections describe how to set up a development environment to get you started.

Using Local Development

This section describes the prerequisites and tools to build a CAP application locally with Eclipse.

Install the CDS Compiler

Also for Java services, you need the CDS command-line tools.
→ Follow the steps in ‘Getting Started’

Install Java & Maven

  1. Download and install SapMachine 11.
  2. Install Apache Maven. Download the Binary zip archive version and save it to a local folder.
  3. Set environment variables for these tools:
    • Windows in cmd prompt
       setx M2_HOME "<maven folder>"
       setx PATH "%M2_HOME%\bin;%PATH%"
      
    • Linux/Mac in ~/.bashrc
      export JAVA_HOME=...
      export M2_HOME=...
      export PATH="$JAVA_HOME/bin:$M2_HOME/bin:$PATH"
      
  4. Log off and on again for these changes to become effective.
  5. Verify that
    • java -version results in a correct version output
    • mvn -version results in a correct version output

Using Eclipse

Install Eclipse

  1. Download the Eclipse package for Java developers.
  2. Unzip and start.

Add SAP and Spring Plug-Ins

The following optional steps make development in Eclipse more convenient:

  1. Install “Spring Tools 4”: From the Eclipse Marketplace (Help -> Eclipse Marketplace…) search and install the “Spring Tools 4”. This provides some convenience when running Spring-based applications and unit tests.

  2. Install the Eclipse plug-in called SAP Cloud Business Application Tools for Eclipse that supports convenient editing of CDS files.

    1. In Eclipse open Help > Install New Software….

    2. Add the URL https://tools.hana.ondemand.com/2019-06/

    3. Expand XS Tools for Eclipse, select SAP Cloud Business Application Tools for Eclipse. SAP Cloud Business Application Tools for Eclipse installation
    4. Click Next and Finish, accept the license agreement and warning about unsigned content, restart Eclipse.

Starting a New Project

Take the following steps to set up a new CAP Java application based on Spring Boot from scratch.

As a prerequisite, you’ve setup your development environment.

To bootstrap a simple CAP application based on Spring Boot, run the following Maven command:

mvn archetype:generate -DarchetypeArtifactId=cds-services-archetype -DarchetypeGroupId=com.sap.cds -DarchetypeVersion=RELEASE -DincludeModel=true

This command generates a runnable “Hello World” CAP application containing a simple CDS model ready for enhancement with the following directory structure.

<PROJECT-ROOT>/
   db/
   srv/

Work in Eclipse

Java Project Structure

The imported Java project has the following structure:

srv/
  src/main/java    Contains Java application logic
  src/gen/java     Contains compiled CDS model and compiled Java PoJos for model entities
  node             Local Node.js installation to run the CDS compiler
  node_modules     Node.js dependencies of the CDS compiler

Hint: To not be distracted by CDS compiler-specific folders in Eclipse, you could define Resource Filters for your project by right-clicking on the project root and selecting “Properties”. Go to “Resource -> Resource Filters” and exclude folders with the name node and node_modules.