Skip to main content

Java – web3j

Overview

Web3j is a Java and Android library for interacting with ethereum nodes.

Resources

Documentation

Installation Guide

Basic Usage

import org.web3j.protocol.Web3j;
import org.web3j.utils.Convert;
import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import java.math.BigInteger;
import java.math.BigDecimal;
import java.io.IOException;

class Main
{
public Main(){}

public void GetAccountBalance()
{
String URL = "https://<CHAIN_PREFIX>.rpc.grove.city/v1/<APP_ID>";
String ACCOUNT = "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae";
Web3j web3j = Web3j.build(new HttpService(URL));

try {
EthGetBalance ethGetBalance = web3j.ethGetBalance(ACCOUNT, DefaultBlockParameterName.LATEST).send();

BigInteger balance = ethGetBalance.getBalance();

System.out.println("Balance in Wei: " + balance);

BigDecimal etherAmount = Convert.fromWei(balance.toString(), Convert.Unit.ETHER);
System.out.println("Balance in Ether :" + etherAmount);
}
catch(IOException e) {
// Code to handle an IOException here
return;
}


}

public static void main(String[] args)
{
//GetAccountBalance();
}
}