Published in


Ethereum for Java Developers

Working with Web3j — A Java library for building on Ethereum

What is Web3j?

web3j is a highly-modular, reactive, type-safe Java and Android library for working with Smart Contracts and integrating with clients (nodes) on the Ethereum network:

What does Web3j support?

  • Complete implementation of Ethereum’s JSON-RPC client API over HTTP and IPC
  • Ethereum wallet support
  • Auto-generation of Java smart contract wrappers to create, deploy, transact with and call smart contracts from native Java code (Solidity and Truffledefinition formats supported)
  • Reactive-functional API for working with filters
  • Ethereum Name Service (ENS) support
  • Support for Parity’s Personal, and Geth’s Personal client APIs
  • Support for ERC20 and ERC721 token standards
  • Comprehensive integration tests demonstrating a number of the above scenarios
  • Command-line tools
  • Android compatible
  • Support for JP Morgan’s Quorum via web3j-quorum


  • Java ≥ version 8
  • Maven
  • Any Java IDE (I am using IntelliJ Community version)
  • Solc (Solidity compiler)
  • A QuikNode (QuikNode.io)
  • Web3j command-line tools
sudo add-apt-repository ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install solc
<!-- https://mvnrepository.com/artifact/org.web3j/core -->
Web3j web3 = Web3j.build(new HttpService("YOUR_QUIKNODE_URL"));Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send();String clientVersion = web3ClientVersion.getWeb3ClientVersion();System.out.println("Client version" + clientVersion);
String walletPassword = "trumpshouldbeimpeached";
String walletDirectory = "/home/batman/wallet/"; //add your own wallet directory path
String walletName = WalletUtils.generateNewWalletFile(walletPassword, new File(walletDirectory));System.out.println(walletName);
Credentials credentials = WalletUtils.loadCredentials(walletPassword, walletDirectory + "/" + walletName);String accountAddress = credentials.getAddress();System.out.println("Account address: " + accountAddress);

Subscribing for Ethereum Events

Subscription subscription = (Subscription) web3.blockFlowable(false).subscribe(block -> {    System.out.println(block.getBlock().getHash());});
Subscription subscription = (Subscription) web3.transactionFlowable().subscribe(tx -> {    System.out.println(tx.getHash());});
Subscription subscription = (Subscription)web3.pendingTransactionFlowable().subscribe(tx -> {   System.out.println(tx.getHash());});
pragma solidity ^0.5.8;contract SimpleBank {
uint8 private clientCount;
mapping (address => uint) private balances;
address public owner;
event LogDepositMade(address indexed accountAddress, uint amount); constructor() public payable {
owner = msg.sender;
clientCount = 0;
function enroll() public returns (uint) {
if (clientCount < 3) {
balances[msg.sender] = 10 ether;
return balances[msg.sender];
function deposit() public payable returns (uint) {
balances[msg.sender] += msg.value;
emit LogDepositMade(msg.sender, msg.value);
return balances[msg.sender];
function withdraw(uint withdrawAmount) public returns (uint remainingBal) {
// Check enough balance available, otherwise just return balance
if (withdrawAmount <= balances[msg.sender]) {
balances[msg.sender] -= withdrawAmount;
return balances[msg.sender];
function balance() public view returns (uint) {
return balances[msg.sender];
function depositsBalance() public view returns (uint) {
return address(this).balance;
$ solc <contract>.sol --bin --abi --optimize -o <output-dir>/
web3j solidity generate -b /path/to/<smart-contract>.bin -a /path/to/<smart-contract>.abi -o /path/to/src/main/java -p com.your.organisation.name
web3j solidity generate -b SimpleBank/SimpleBank.bin -a SimpleBank/SimpleBank.abi -o ~/IdeaProjects/quiknode/src/main/java/ -p com.simplebank
SimpleBank simpleBank = SimpleBank.load("0x7208b4883a4dF0F3e5cD084F2F9C579e7b622A38",web3, credentials , new DefaultGasProvider());
simpleBank.logDepositMadeEventFlowable(DefaultBlockParameterName.EARLIEST,DefaultBlockParameterName.LATEST).doOnError(error -> System.err.println("The error message is: " + error.getMessage()))        .subscribe(logDepositMadeEventResponse ->                System.out.println(logDepositMadeEventResponse.amount));

About QuikNode



A platform to access ETH, BTC, BSC, MATIC, OPTIMISM, ARBITRUM, FTM, CELO, SOL, TERRA & xDAI blockchains via API. Infra & tools to build + scale your #Web3 dApp!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store