-
Web3J is a Java and Android library for interacting with nodes and smart contracts on the Ethereum network.
- Add the following dependencies to Maven to start using Web3J:
// Java 8
<dependency> <groupId>org.web3j</groupId> <artifactId>core</artifactId> <version>3.3.1</version> </dependency>
// Android
<dependency> <groupId>org.web3j</groupId> <artifactId>core</artifactId> <version>3.3.1-android</version> </dependency>
- If you are using Gradle, then use the following methods:
// Java 8
compile ('org.web3j:core:3.3.1')
// Android
compile ('org.web3j:core:3.3.1-android')
- To connect to a node and initialize an object, use Web3J.build. It defaults to localhost:8545 and can be changed:
Web3j web3 = Web3j.build(new HttpService());
- To send a synchronous request using Web3J, use the following method:
Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
- To make it asynchronous, use the following method:
Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().sendAsync().get();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
More information about using this library is available in the official documentation at https://github.com/web3j/web3j.