All Products
Search
Document Center

Blockchain as a Service:Use Fabric Java SDK

Last Updated:Apr 29, 2020

Install java and maven

The sample program relies on the Java 1.8 runtime environment and Maven, which can be confirmed by the following command.

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

Download sample program

Please download java-sdk-demo-1.4.5. After downloading and extracting, copy the SDK configuration file connection-profile-standard.yaml to java-sdk-demo.

The final directory structure is as follows:

java sdk source

Install dependencies

It contains the jar file of the fabric-sdk-java and the corresponding source package. Can be installed by the following:

java-sdk-demo-1.4.5 run following commands:

  1. 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
  2. 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

Deploy chaincode

Upload the chaincode chaincode/sacc.out to BaaS and instantiate it. For the upload method, please see Deploy Chaincode.

Running the sample program

Open a Java IDE (Take IDEA for example) and import the java-sdk-demo project.

Open the file src/main/java/com/aliyun/baas/Main.java, modify the value of the following variables.

  • channelName: the name of the channel, such as “first-channel”.
  • userName: Username.
  • secret: User’s password. If you forget the password, you can reset the password at first.
  • chaincodeName: The chaincode name. If you use sacc.out, please enter sacc.
  • chaincodeVersion: The version of the chaincode. If you use sacc.out, please enter 1.0.0.

after modification, run com.aliyun.baas.Main as main class:

java sdk

This sample program based on Hyperledger Fabric java SDK, mainly to help you understand the basic use of the java SDK, which demonstrates the following:

  1. Enroll user.
  2. Read the configuration file, connect to the channel-related peer, and listen for block events.
  3. Get the block information of the ledger and print to console.
  4. Invoke the sacc chaincode, write a new ledger record, and read it.
  5. Disconnect from the peer.

More instructions on using the Java SDK can be found at fabric-sdk-java.