github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/docs/Setup/JAVAChaincode.md (about)

     1  ## Java chaincode
     2  
     3  Note: This guide generally assumes you have followed the Chaincode development environment setup tutorial [here](https://github.com/hyperledger/fabric/blob/master/docs/Setup/Chaincode-setup.md).
     4  
     5  ### To get started developing Java chaincode
     6  
     7  1. Ensure you have gradle
     8    * Download the binary distribution from [http://gradle.org/gradle-download/](http://gradle.org/gradle-download/)
     9    * Unpack, move to the desired location, and add gradle's bin directory to your system path
    10    * Ensure `gradle -v` works from the command-line, and shows version 2.12 or greater
    11    * Optionally, enable the [gradle daemon](https://docs.gradle.org/current/userguide/gradle_daemon.html) for faster builds
    12  2. Ensure you have the Java 1.8 **JDK** installed. Also ensure Java's directory is on your path with `java -version`
    13    * Additionally, you will need to have the [`JAVA HOME`](https://docs.oracle.com/cd/E19182-01/821-0917/6nluh6gq9/index.html) variable set to your **JDK** installation in your system path
    14  3. From your command line terminal, move to the `devenv` subdirectory of your workspace environment. Log into a Vagrant terminal by executing the following command:
    15  
    16      ```
    17      vagrant ssh
    18      ```
    19  
    20  4. Build and run the peer process.
    21  
    22      ```
    23      cd $GOPATH/src/github.com/hyperledger/fabric
    24      make peer
    25      peer node start
    26      ```
    27  
    28  5. The following steps is for deploying chaincode in non-dev mode.
    29  
    30  	* Deploy the chaincode,
    31  
    32  ```
    33  	peer chaincode deploy -l java -p /opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/java/SimpleSample -c '{"Args": ["init", "a","100", "b", "200"]}'
    34  ```
    35  
    36  `6d9a704d95284593fe802a5de89f84e86fb975f00830bc6488713f9441b835cf32d9cd07b087b90e5cb57a88360f90a4de39521a5595545ad689cd64791679e9`
    37  
    38  		* This command will give the 'name' for this chaincode, and use this value in all the further commands with the -n (name) parameter
    39  
    40  
    41  		* PS. This may take a few minutes depending on the environment as it deploys the chaincode in the container,
    42  
    43  * Invoke a transfer transaction,
    44  
    45  ```
    46  	peer chaincode invoke -l java \
    47  	-n 6d9a704d95284593fe802a5de89f84e86fb975f00830bc6488713f9441b835cf32d9cd07b087b90e5cb57a88360f90a4de39521a5595545ad689cd64791679e9 \
    48  	-c '{"Args": ["transfer", "a", "b", "10"]}'
    49  ```
    50  `c7dde1d7-fae5-4b68-9ab1-928d61d1e346`
    51  
    52  * Query the values of a and b after the transfer
    53  
    54  ```
    55  	peer chaincode query -l java \
    56  	-n 6d9a704d95284593fe802a5de89f84e86fb975f00830bc6488713f9441b835cf32d9cd07b087b90e5cb57a88360f90a4de39521a5595545ad689cd64791679e9 \
    57  	-c '{ "Args": ["query", "a"]}'
    58  	{"Name":"a","Amount":"80"}
    59  
    60  
    61  	peer chaincode query -l java \
    62  	-n 6d9a704d95284593fe802a5de89f84e86fb975f00830bc6488713f9441b835cf32d9cd07b087b90e5cb57a88360f90a4de39521a5595545ad689cd64791679e9 \
    63  	-c '{ "Args": ["query", "b"]}'
    64  	{"Name":"b","Amount":"220"}
    65  ```
    66  
    67  
    68  ### Java chaincode deployment in DEV Mode
    69  
    70  1. Follow the step 1 to 3 as above,
    71  2. Build and run the peer process
    72  ```    
    73      cd $GOPATH/src/github.com/hyperledger/fabric
    74      make peer
    75      peer node start --peer-chaincodedev
    76  ```
    77  3. Open the second Vagrant terminal and build the Java shim layer and publish it to Local Maven Repo
    78  ```
    79  cd $GOPATH/src/github.com/hyperledger/fabric/core/chaincode/shim/java
    80  gradle -b build.gradle clean
    81  gradle -b build.gradle build
    82  ```
    83  4. Change to examples folder to build and run,
    84  ```
    85      cd $GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/java/SimpleSample
    86      gradle -b build.gradle build
    87  ```
    88  5. Run the SimpleSample chaincode using the `gradle -b build.gradle run`
    89  
    90  6. Open the third Vagrant terminal to run init and invoke on the chaincode
    91  
    92      peer chaincode deploy -l java -n SimpleSample -c '{"Args": ["init", "a","100", "b", "200"]}'
    93  ```
    94  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 []}
    95  19:10:15.461 [crypto] main -> INFO 002 Log level recognized 'info', set to INFO
    96  SimpleSample
    97  ```
    98      peer chaincode invoke -l java -n SimpleSample -c '{"Args": ["transfer", "a", "b", "10"]}'
    99  
   100  ```
   101  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 []}
   102  19:11:13.553 [crypto] main -> INFO 002 Log level recognized 'info', set to INFO
   103  978ff89e-e4ef-43da-a9f8-625f2f6f04e5
   104  ```
   105      peer chaincode query -l java -n SimpleSample -c '{ "Args": ["query", "a"]}'
   106  ```
   107  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 []}
   108  19:12:19.289 [crypto] main -> INFO 002 Log level recognized 'info', set to INFO
   109  {"Name":"a","Amount":"90"}
   110  ```
   111      peer chaincode query -l java -n SimpleSample -c '{"Args": ["query", "b"]}'
   112  ```
   113  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 []}
   114  19:12:25.667 [crypto] main -> INFO 002 Log level recognized 'info', set to INFO
   115  {"Name":"b","Amount":"210"}
   116  ```
   117  
   118  ### Developing new JAVA chaincode
   119  1. Create a new Java project structure.
   120  2. Use existing `build.grade` from  any example JAVA Chaincode project like `examples/chaincode/java/SimpleSample`.
   121  3. Make your main class extend ChaincodeBase class and implement the following methods from base class.
   122    1. `public String run(ChaincodeStub stub, String function, String[] args)   `
   123    2. `public String query(ChaincodeStub stub, String function, String[] args)`
   124    3. `public String getChaincodeID()`
   125  4. Modify the `mainClassName` in `build.gradle` to point to your new class.
   126  5. Build this project using `gradle -b build.gradle build`
   127  6. Run this chaincode after starting a peer in dev-mode as above using `gradle -b build.gradle run`
   128  
   129