github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/docs/source/Setup/JAVAChaincode.rst (about) 1 Java chaincode 2 -------------- 3 4 Note: This guide generally assumes you have followed the :doc:`Chaincode 5 development environment setup tutorial <Chaincode-setup>`. 6 7 To get started developing Java chaincode 8 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 9 10 1. Ensure you have gradle 11 12 - Download the binary distribution from 13 http://gradle.org/gradle-download/ 14 - Unpack, move to the desired location, and add gradle's bin directory 15 to your system path 16 - Ensure ``gradle -v`` works from the command-line, and shows version 17 2.12 or greater 18 - Optionally, enable the `gradle 19 daemon <https://docs.gradle.org/current/userguide/gradle_daemon.html>`__ 20 for faster builds 21 22 2. Ensure you have the Java 1.8 **JDK** installed. Also ensure Java's 23 directory is on your path with ``java -version`` 24 25 - Additionally, you will need to have the |JAVA_HOME|_ variable set to your 26 **JDK** installation in your system path 27 28 3. From your command line terminal, move to the ``devenv`` subdirectory 29 of your workspace environment. Log into a Vagrant terminal by 30 executing the following command: 31 32 .. |JAVA_HOME| replace:: ``JAVA_HOME`` 33 .. _JAVA_HOME: https://docs.oracle.com/cd/E19182-01/821-0917/6nluh6gq9/index.html 34 35 :: 36 37 vagrant ssh 38 39 4. Build and run the peer process. 40 41 :: 42 43 cd $GOPATH/src/github.com/hyperledger/fabric 44 make peer 45 peer node start 46 47 5. The following steps is for deploying chaincode in non-dev mode. 48 49 - Deploy the chaincode, 50 51 :: 52 53 peer chaincode deploy -l java -p /opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/java/SimpleSample -c '{"Args": ["init", "a","100", "b", "200"]}' 54 55 ``6d9a704d95284593fe802a5de89f84e86fb975f00830bc6488713f9441b835cf32d9cd07b087b90e5cb57a88360f90a4de39521a5595545ad689cd64791679e9`` 56 57 :: 58 59 * This command will give the 'name' for this chaincode, and use this value in all the further commands with the -n (name) parameter 60 61 62 * PS. This may take a few minutes depending on the environment as it deploys the chaincode in the container, 63 64 - Invoke a transfer transaction, 65 66 :: 67 68 peer chaincode invoke -l java \ 69 -n 6d9a704d95284593fe802a5de89f84e86fb975f00830bc6488713f9441b835cf32d9cd07b087b90e5cb57a88360f90a4de39521a5595545ad689cd64791679e9 \ 70 -c '{"Args": ["transfer", "a", "b", "10"]}' 71 72 ``c7dde1d7-fae5-4b68-9ab1-928d61d1e346`` 73 74 - Query the values of a and b after the transfer 75 76 :: 77 78 peer chaincode query -l java \ 79 -n 6d9a704d95284593fe802a5de89f84e86fb975f00830bc6488713f9441b835cf32d9cd07b087b90e5cb57a88360f90a4de39521a5595545ad689cd64791679e9 \ 80 -c '{ "Args": ["query", "a"]}' 81 {"Name":"a","Amount":"80"} 82 83 84 peer chaincode query -l java \ 85 -n 6d9a704d95284593fe802a5de89f84e86fb975f00830bc6488713f9441b835cf32d9cd07b087b90e5cb57a88360f90a4de39521a5595545ad689cd64791679e9 \ 86 -c '{ "Args": ["query", "b"]}' 87 {"Name":"b","Amount":"220"} 88 89 Java chaincode deployment in DEV Mode 90 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 91 92 1. Follow the step 1 to 3 as above, 93 2. Build and run the peer process 94 95 :: 96 97 cd $GOPATH/src/github.com/hyperledger/fabric 98 make peer 99 peer node start --peer-chaincodedev 100 101 3. Open the second Vagrant terminal and build the Java shim layer and 102 publish it to Local Maven Repo 103 104 :: 105 106 cd $GOPATH/src/github.com/hyperledger/fabric/core/chaincode/shim/java 107 gradle -b build.gradle clean 108 gradle -b build.gradle build 109 110 4. Change to examples folder to build and run, 111 112 :: 113 114 cd $GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/java/SimpleSample 115 gradle -b build.gradle build 116 117 5. Run the SimpleSample chaincode using the 118 ``gradle -b build.gradle run`` 119 120 6. Open the third Vagrant terminal to run init and invoke on the 121 chaincode 122 123 peer chaincode deploy -l java -n SimpleSample -c '{"Args": ["init", 124 "a","100", "b", "200"]}' 125 126 :: 127 128 2016/06/28 19:10:15 Load docker HostConfig: %+v &{[] [] [] [] false map[] [] false [] [] [] [] host { 0} [] { map[]} false [] 0 0 0 false 0 0 0 0 []} 129 19:10:15.461 [crypto] main -> INFO 002 Log level recognized 'info', set to INFO 130 SimpleSample 131 132 peer chaincode invoke -l java -n SimpleSample -c '{"Args": 133 ["transfer", "a", "b", "10"]}' 134 135 :: 136 137 2016/06/28 19:11:13 Load docker HostConfig: %+v &{[] [] [] [] false map[] [] false [] [] [] [] host { 0} [] { map[]} false [] 0 0 0 false 0 0 0 0 []} 138 19:11:13.553 [crypto] main -> INFO 002 Log level recognized 'info', set to INFO 139 978ff89e-e4ef-43da-a9f8-625f2f6f04e5 140 141 :: 142 143 peer chaincode query -l java -n SimpleSample -c '{ "Args": ["query", "a"]}' 144 145 :: 146 147 2016/06/28 19:12:19 Load docker HostConfig: %+v &{[] [] [] [] false map[] [] false [] [] [] [] host { 0} [] { map[]} false [] 0 0 0 false 0 0 0 0 []} 148 19:12:19.289 [crypto] main -> INFO 002 Log level recognized 'info', set to INFO 149 {"Name":"a","Amount":"90"} 150 151 :: 152 153 peer chaincode query -l java -n SimpleSample -c '{"Args": ["query", "b"]}' 154 155 :: 156 157 2016/06/28 19:12:25 Load docker HostConfig: %+v &{[] [] [] [] false map[] [] false [] [] [] [] host { 0} [] { map[]} false [] 0 0 0 false 0 0 0 0 []} 158 19:12:25.667 [crypto] main -> INFO 002 Log level recognized 'info', set to INFO 159 {"Name":"b","Amount":"210"} 160 161 Developing new JAVA chaincode 162 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 163 164 1. Create a new Java project structure. 165 2. Use existing ``build.grade`` from any example JAVA Chaincode project 166 like ``examples/chaincode/java/SimpleSample``. 167 3. Make your main class extend ``ChaincodeBase`` class and implement the 168 following methods from base class. 169 170 - ``public Response init(ChaincodeStub stub)`` 171 - ``public Response invoke(ChaincodeStub stub);`` 172 - ``public String getChaincodeID()`` 173 174 4. Modify the ``mainClassName`` in ``build.gradle`` to point to your new 175 class. 176 5. Build this project using ``gradle -b build.gradle build`` 177 6. Run this chaincode after starting a peer in dev-mode as above using 178 ``gradle -b build.gradle run`` 179 180 .. Licensed under Creative Commons Attribution 4.0 International License 181 https://creativecommons.org/licenses/by/4.0/ 182