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.
$ mvn -version
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:05+08:00)
...
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:
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:
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
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:
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:
- Enroll user.
- Read the configuration file, connect to the channel-related peer, and listen for block events.
- Get the block information of the ledger and print to console.
- Invoke the
sacc
chaincode, write a new ledger record, and read it. - Disconnect from the peer.
More instructions on using the Java SDK can be found at fabric-sdk-java.