<p>The class <a href="https://code.google.com/p/bitcoinj/source/browse/core/src/main/java/com/google/bitcoin/kits/WalletAppKit.java%20WalletAppKit" rel="nofollow">WalletAppKit</a> is a good start. Here's a snippet of code that dumps the most recent block, you can probably take it from here. It will take a while to download the block chain and then it will print out the most recent transactions.</p>

<pre><code>public class DumpLastBlock {
public static void main(String args[]) throws Exception {
    WalletAppKit kit = new WalletAppKit(MainNetParams.get(), new java.io.File("."), "test");
    kit.startAndWait();
    BlockChain chain = kit.chain();
    BlockStore bs = chain.getBlockStore();
    Peer peer = kit.peerGroup().getDownloadPeer();
    Block b = peer.getBlock(bs.getChainHead().getHeader().getHash()).get();
    System.out.println(b);
}
}
</code></pre>

CC BY-SA 3.0
2014-02-15T22:19:04.893Z
2014-02-15T22:19:04.893Z
Diego Basch
<p>I'm sorry everyone, I have looked and stared at the <a href="http://bitcoinj.googlecode.com/git-history/78bdd09189ec3d7d38c64029b9742e24f2bfc143/target/site/apidocs/index.html" rel="nofollow noreferrer">API</a> and I'm coming up with nothing. </p>

<p>I've got a public static void main (naturally), but have no idea how to:</p>

<ol>
<li>Connect to the network</li>
<li>Download the block chain</li>
<li>Navigate through it.</li>
</ol>

<p>The purpose of this is to keep the block chain as a record of transactions and perform reporting on my own transactions. I don't have my wallet on the same machine. </p>

<p>Unfortunately for me, my ability to read the api is still underdeveloped, and <a href="https://code.google.com/p/bitcoinj/wiki/GettingStarted" rel="nofollow noreferrer">the</a> <a href="https://bitcoin.stackexchange.com/questions/7722/test-network-genesis-block">various</a> pages I've seen don't really go step by dummy step. </p>

<p>Most important of the above steps is, how to get the block chain...</p>

<bitcoinj>
Using bitcoinj to discover peers and download the block chain
TheMightyLlama
# Using bitcoinj to discover peers and download the block chain

(Asked by: username_0 on 2014-01-26)

I'm sorry everyone, I have looked and stared at the [API](http://bitcoinj.googlecode.com/git-history/78bdd09189ec3d7d38c64029b9742e24f2bfc143/target/site/apidocs/index.html) and I'm coming up with nothing.

I've got a public static void main (naturally), but have no idea how to:

1. Connect to the network
2. Download the block chain
3. Navigate through it.

The purpose of this is to keep the block chain as a record of transactions and perform reporting on my own transactions. I don't have my wallet on the same machine.

Unfortunately for me, my ability to read the api is still underdeveloped, and [the](https://code.google.com/p/bitcoinj/wiki/GettingStarted) [various](https://bitcoin.stackexchange.com/questions/7722/test-network-genesis-block) pages I've seen don't really go step by dummy step.

Most important of the above steps is, how to get the block chain...

**Tags:** bitcoinj


---

## 1 answers

### Answer by username_1 on 2014-02-15. Score: 3

The class WalletAppKit is a good start. Here's a snippet of code that dumps the most recent block, you can probably take it from here. It will take a while to download the block chain and then it will print out the most recent transactions.
public class DumpLastBlock {
public static void main(String args[]) throws Exception {
    WalletAppKit kit = new WalletAppKit(MainNetParams.get(), new java.io.File("."), "test");
    kit.startAndWait();
    BlockChain chain = kit.chain();
    BlockStore bs = chain.getBlockStore();
    Peer peer = kit.peerGroup().getDownloadPeer();
    Block b = peer.getBlock(bs.getChainHead().getHeader().getHash()).get();
    System.out.println(b);
}
}


---

