All Products
Search
Document Center

Blockchain as a Service:Java SDK usage example

Last Updated:Mar 31, 2026

This tutorial walks you through running the Fabric Java SDK sample program against your Blockchain as a Service (BaaS) network. The sample program demonstrates five operations in sequence: enrolling a user, connecting to a channel, reading ledger block information, invoking the sacc chaincode to write and read a ledger entry, and disconnecting from the peer.

What the sample demonstrates

When the program runs successfully, it performs these operations in order:

  1. Enroll user — authenticates with the network using userName and secret.

  2. Connect to the channel — reads connection-profile-standard.yaml, connects to the channel's peer nodes, and starts listening for block events.

  3. Read block information — retrieves block data from the ledger and prints it to the console.

  4. Invoke the sacc chaincode — writes a new ledger entry and reads it back.

  5. Disconnect — closes the connection to the peer.

Prerequisites

Before you begin, ensure that you have:

  • A BaaS network with at least one channel created

  • A user account and password for that network

  • Java 1.8 runtime environment and Apache Maven installed

  • The connection-profile-standard.yaml SDK configuration file downloaded from the BaaS console

Verify your Java and Maven installation:

mvn -version

The output should show Apache Maven 3.5.3 and Java version 1.8.x:

Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:05+08:00)
...
Java version: 1.8.0_161, vendor: Oracle Corporation

Step 1: Download the sample program

Download java-sdk-demo-1.4.5 and extract it. Copy the SDK configuration file connection-profile-standard.yaml into the extracted java-sdk-demo directory.

After extraction and copying, the directory structure looks like this:

java-sdk-demo-1.4.5/
├── chaincode/
│   └── sacc.out
├── lib/
│   ├── fabric-sdk-java-1.4.5-jar-with-dependencies.jar
│   └── fabric-sdk-java-1.4.5-sources.jar
├── src/
│   └── main/java/com/aliyun/baas/
│       └── Main.java
└── connection-profile-standard.yaml   (copied from BaaS console)

Step 2: Install dependencies

The sample ships with the Fabric Java SDK JAR files under ./lib/. From inside the java-sdk-demo-1.4.5 directory, run both Maven install commands to register them in your local Maven repository:

mvn install:install-file \
  -Dfile=./lib/fabric-sdk-java-1.4.5-jar-with-dependencies.jar \
  -DgroupId=org.hyperledger.fabric-sdk-java \
  -DartifactId=fabric-sdk-java \
  -Dversion=1.4.5 \
  -Dpackaging=jar

mvn install:install-file \
  -Dfile=./lib/fabric-sdk-java-1.4.5-sources.jar \
  -DgroupId=org.hyperledger.fabric-sdk-java \
  -DartifactId=fabric-sdk-java \
  -Dversion=1.4.5 \
  -Dpackaging=jar \
  -Dclassifier=sources

Step 3: Deploy the chaincode

Upload chaincode/sacc.out to BaaS and instantiate it. For instructions, see Deploy Chaincode.

Step 4: Configure and run the sample

  1. Open the java-sdk-demo project in a Java IDE (IntelliJ IDEA, for example).

  2. Open src/main/java/com/aliyun/baas/Main.java and set the following variables:

    VariableDescriptionWhere to find itExample
    channelNameThe name of the channel to connect toBaaS console > Channels"first-channel"
    userNameYour BaaS user account nameBaaS console > Users
    secretYour BaaS user password. If you forgot the password, reset it from the BaaS console before proceeding.BaaS console > Users
    chaincodeNameThe name of the instantiated chaincodesacc when using sacc.out"sacc"
    chaincodeVersionThe version of the instantiated chaincode1.0.0 when using sacc.out"1.0.0"
  3. Run com.aliyun.baas.Main as the main class.

What's next

For the full Fabric Java SDK API reference and advanced usage, see the fabric-sdk-java repository.