Generate a Java Wrapper from your Smart Contract
In this article, we discover how to generate a Java Wrapper Class directly from a smart contract to interact with a smart contract in Java.
There are different methods to generate a Java Wrapper Class from a Smart Contract:
- The Web3j Command Line tool and solc
- The Web3j Command Line tool and the artifacts generated by a Truffle build
- The web3j-maven-plugin
- The web3j-gradle-plugin
To show how to use the methods above, this tutorial uses the following Smart Contract which notarizes documents into a registry on the Ethereum Blockchain.
DocumentRegistry.sol
1 | pragma solidity ^0.5.6; |
Method 1 - Web3j Command Line tool and solc
This first method generates the Smart contract ABI and bytecode from with solc
and gives those two files as input to web3j-cli
to generate the Wrapper.
1. Install solc and verify the version
Install solc and run the command below to make sure the solc version is greater than or equal to 0.5.6
(the version specified in the smart contract).
1 | solc --version |
2. Install web3j-cli
To install the web3j-cli, download a zipfile/tarball from the releases page of the project repository, under the Downloads section, or for macOS users via Homebrew, or for Arch linux users via the AUR.
1 | brew tap web3j/web3j |
To run via a zipfile, extract it and run the binary, you may also want to add the binary to your PATH
:
1 | unzip web3j-4.3.0.zip |
3. Compile the smart contract with solc
Given our Solidity file DocumentRegistry.sol, the solc <sol> --bin --abi --optimize -o <output>
command compiles the smart contract and generates two new files in the same directory :
1 | solc DocumentRegistry.sol --bin --abi --optimize -o ./ |
- DocumentRegistry.bin: Binary file, bytecode of the smart contract
- DocumentRegistry.abi: ABI (Application Binary Interface) of the smart contract which defines the interface in a JSON format.
4. Generate the Wrapper with the web3j-cli
Using the ABI and bytecode (generated in step 3) and web3j-cli
(installed during step 2), we can now generate our Smart contract Java Wrapper with the following command:
1 | web3j solidity generate -a=<abiFile> -b=<binFile> -o=<destinationFileDir> -p=<packageName> |
For example:
1 | web3j solidity generate -a DocumentRegistry.abi -b DocumentRegistry.bin -o . -p me.gjeanmart.tutorials.javaethereum.wrapper |
As a result, you should see the Java Wrapper file generated into the folder
1 | ./me/gjeanmart/tutorials/javaethereum/wrapper/DocumentRegistry.java |
Method 2 - Web3j Command Line tool and Truffle artefacts
Truffle is one of the most well-known frameworks to help you develop, test and deploy with Ethereum. We can use the artefacts that Truffle generates with the Web3j command line tool to create the wrapper class.
1. Install Truffle
Truffle is available as an npm package.
1 | npm install truffle -g |
2. Initialize a new Truffle project
To initialize a Truffle project, use the command truffle init
in a new folder. The command creates the folders contracts/, migration/ and test/, and the file truffle-config.js.
1 | mkdir truffle |
3. Add the contract into the folder contracts
Copy the Smart Contract source DocumentRegistry.sol into the folder contracts.
4. Compile the contract
Compile the smart contract with the command truffle compile
, this command generates a new folder build/contracts/, containing a Truffle artefact for each Smart contract compiled.
1 | truffle compile |
5. Generate the Smart Contract Java Wrapper from the Truffle Artefact
Finally, web3j-cli provides a method to generate the Wrapper directly from the Truffle artefact result of truffle compile
with the command:
1 | web3j truffle generate ./build/contracts/DocumentRegistry.json -o . -p me.gjeanmart.tutorials.javaethereum.wrapper |
As a result, you should see the Java Wrapper file generated into the folder _<package_folders>/
1 | ./me/gjeanmart/tutorials/javaethereum/wrapper/DocumentRegistry.java |
Note: With Truffle you can do a lot more than shown in this post, such as deployment scriptd (migration), Multi-network configuration, testing, debugging. I recommend reading the following guide to learn more about all the features.
Method 3 - web3j-maven-plugin
The next solution is more elegant than the previous two because we don’t have to install the webj-cli and copy the file to the source folder. We can use this method directly inside a Java project using Maven and the web3j-maven-plugin. The following steps assume you have created a Maven project.
1. Prerequisites
Install solc and run the command below to make sure the solc version is greater than or equal to 0.5.6
(the version specified in the smart contract).
1 | solc --version |
2. Copy the smart contract into the folder src/main/resources
Copy the Smart Contract source DocumentRegistry.sol into the src/main/resources folder of the Maven project.
3. Configure Maven to generate the Wrapper during the generate-sources
phase
In this step, we configure two Maven plugins:
web3j-maven-plugin
The first plugin does the same as the two previous methods but integrated with Maven. First we configure the plugin to execute automatically when entering the generate-sources
phase of the project.
Secondly we configure the plugin parameters:
- packageName: Package name to apply to the generated Wrapper classes
- sourceDestination: Target destination folder to move the generated Wrapper classes
- soliditySourceFiles: Where to find the Smart Contract source files
build-helper-maven-plugin
The second plugin adds the sourceDestination folder into the classpath so we can import the generated Wrapper classes
pom.xml
1 | <build> |
4. Run Maven generate-sources
Finally, build the Maven project by using, for example mvn clean package
(including the generate-sources phase). As a result, we can see the Java Wrapper has been generated into /target/generated-sources/contracts/me/gjeanmart/tutorials/javaethereum/contracts/generated/DocumentRegistry.java
and added to the classpath automatically.
Method 4 - web3j-gradle-plugin
The last method is similar to the previous method with Maven, but using Gradle instead.
1. Prerequisites
Install solc and run the command below to make sure the solc version is greater than or equal to 0.5.6
(the version specified in the smart contract).
1 | solc --version |
2. Place the smart contract into the folder src/main/solidity
Copy the Smart Contract source DocumentRegistry.sol into the folder src/main/solidity of the Gradle project.
3. Configure Gradle to generate the Wrapper during build
First import the web3j-gradle plugin into the build.gradle file
1 | plugins { |
Then we can configure the plugin to specify the package name and the target folder for the generated wrapper classes:
1 | web3j { |
To use your system installed version of solc
instead of the version bundled with the plugin, add the following lines to build.gradle:
1 | solidity { |
build.gradle
1 | /* |
4. Execute gradle build
In this last step, we execute the build using ./gradlew tasks --all
and verify that our generated wrapper classes have been generated.