Building and Testing an Ethereum Transfer with Java and Web3j on a Local Ganache Testnet
This article walks through setting up a local Ethereum testnet using Ganache, configuring a Java project with Web3j, and implementing code to query the network and perform a signed Ether transfer, providing step-by-step instructions, code snippets, and screenshots for beginners.
The author, new to blockchain, describes learning Ethereum basics and then setting up a local test network (Ganache) to perform the first Ether transfer using Java.
First, a Ganache test chain is installed and started, providing a local HTTP endpoint (host and port) that mimics an Ethereum node. The Quickstart mode displays the main interface, and the host URL (e.g., http://127.0.0.1:7545 ) is used in subsequent code.
Next, a Java project is created using the Web3j library, which offers JSON‑RPC based interaction with Ethereum. The Maven dependency required is:
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.3.0</version>
</dependency>A simple class with a main method is added to connect to the Ganache node via Web3j.build(new HttpService("http://127.0.0.1:7545")) and to call ethProtocolVersion() , printing the returned JSON to confirm the environment works.
For the transfer demo, the code creates credentials from a private key, obtains the nonce, builds a raw Ether transaction, signs it, and sends it with ethSendRawTransaction . It then polls ethGetTransactionReceipt until the receipt is available, finally printing "deal confirm" to indicate success.
package com.funtester.ethereum;
import com.funtester.frame.SourceCode;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.*;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Convert;
import org.web3j.utils.Numeric;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Optional;
public class Demo extends SourceCode {
static Web3j drive = Web3j.build(new HttpService("http://127.0.0.1:7545"));
public static void main(String[] args) throws IOException, InterruptedException {
String privateKey = "0x04c02dc2381e1f69a274fa7cc4851c9eb01358fae8753dd4273014b43e34ab0c";
Credentials credentials = Credentials.create(privateKey);
EthGetTransactionCount ethGetTransactionCount = drive.ethGetTransactionCount(credentials.getAddress(), DefaultBlockParameterName.LATEST).send();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
String recipientAddress = "0x9483df5Bb8183548B4D7eb66B44C41FccD7c40A6";
BigInteger value = Convert.toWei("1", Convert.Unit.ETHER).toBigInteger();
BigInteger gasLimit = BigInteger.valueOf(22100);
BigInteger gasPrice = Convert.toWei("11", Convert.Unit.GWEI).toBigInteger();
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
nonce,
gasPrice,
gasLimit,
recipientAddress,
value);
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction ethSendTransaction = drive.ethSendRawTransaction(hexValue).send();
String transactionHash = ethSendTransaction.getTransactionHash();
Optional
transactionReceipt = null;
do {
EthGetTransactionReceipt ethGetTransactionReceiptResp = drive.ethGetTransactionReceipt(transactionHash).send();
transactionReceipt = ethGetTransactionReceiptResp.getTransactionReceipt();
Thread.sleep(3000);
} while (!transactionReceipt.isPresent());
System.out.println("deal confirm");
}
}The console output shows a JSON response confirming the protocol version, and the final "deal confirm" message indicates the transaction has been mined. Users can verify the balance change in Ganache or query the account via Ethereum APIs.
Overall, the article provides a practical, beginner‑friendly guide to setting up a local Ethereum environment, writing Java code with Web3j, and executing a signed Ether transfer.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.