Bitcoin programming with BitcoinJ Java Library

In this tutorial, we are going to explore the BitcoinJ library to interact with the Bitcoin network.

java --version

java 11.0.16 2022-07-19 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.16+11-LTS-199)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.16+11-LTS-199, mixed mode)

mvn --version

Apache Maven 3.8.6
Maven home: /Users/binarycipher/Documents/apache-maven-3.8.6
Java version: 11.0.16, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-11.0.16.jdk/Contents/Home
Default locale: en_CA, platform encoding: UTF-8
OS name: "mac os x", version: "13.0", arch: "aarch64", family: "mac"

Installing BitcoinJ

Using BitcoinJ library will connect to the bitcoin network and perform the functions in the library to send and receive bitcoins in real-time on the blockchain.

To set up the bitcoinj project, you will first need to make sure you have the Java Development Kit (JDK) installed on your system. You can check if you have the JDK installed by running the following command in a terminal window:

Copy codejava -version

If the JDK is not installed, you can download and install it from the Java website.

Once you have the JDK installed, you can download the latest version of bitcoinj from the project’s GitHub page. You can download the source code as a ZIP file or clone the repository using Git.

Once you have the source code, you can build the project using Gradle, which is a build tool for Java. To do this, open a terminal window and navigate to the directory where you downloaded the bitcoinj source code. Then, run the following command to build the project:

Copy code./gradlew build

This will compile the source code and create the necessary JAR files. You can then use bitcoinj in your own projects by including the JAR files in your classpath.

Let’s make a new Bitcoin Wallet using BitcoinJ

Java program that uses BitcoinJ to create a Bitcoin wallet, generate a new Bitcoin address, and display the address.

import org.bitcoinj.core.*;
import org.bitcoinj.kits.WalletAppKit;
import org.bitcoinj.params.MainNetParams;

public class BitcoinWalletExample {

    public static void main(String[] args) {
        // NetworkParameters for the Bitcoin main network
        NetworkParameters networkParameters = MainNetParams.get();

        // Directory where the wallet file will be stored
        String walletDirectory = System.getProperty("user.home") + "/bitcoinj-example";

        // Create a new WalletAppKit
        WalletAppKit kit = new WalletAppKit(networkParameters, new File(walletDirectory), "mywallet") {
            protected void onSetupCompleted() {
                if (wallet().getActiveKeyChain().getIssuedReceiveKeys().isEmpty()) {
                    // Generate a new Bitcoin address
                    Address address = wallet().freshReceiveAddress();
                    System.out.println("Your new Bitcoin address: " + address.toString());

        // Start the kit and sync with the network

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks