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:
Enroll user — authenticates with the network using
userNameandsecret.Connect to the channel — reads
connection-profile-standard.yaml, connects to the channel's peer nodes, and starts listening for block events.Read block information — retrieves block data from the ledger and prints it to the console.
Invoke the sacc chaincode — writes a new ledger entry and reads it back.
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.yamlSDK configuration file downloaded from the BaaS console
Verify your Java and Maven installation:
mvn -versionThe 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 CorporationStep 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=sourcesStep 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
Open the
java-sdk-demoproject in a Java IDE (IntelliJ IDEA, for example).Open
src/main/java/com/aliyun/baas/Main.javaand set the following variables:Variable Description Where to find it Example channelNameThe name of the channel to connect to BaaS console > Channels "first-channel"userNameYour BaaS user account name BaaS 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 chaincode saccwhen usingsacc.out"sacc"chaincodeVersionThe version of the instantiated chaincode 1.0.0when usingsacc.out"1.0.0"Run
com.aliyun.baas.Mainas the main class.
What's next
For the full Fabric Java SDK API reference and advanced usage, see the fabric-sdk-java repository.