<p><a href="https://github.com/bitcoinj/bitcoinj/blob/master/examples/src/main/java/org/bitcoinj/examples/FetchBlock.java" rel="nofollow noreferrer">Example</a> in bitcoinj source code is exactly what you are looking for:</p>

<pre><code>BriefLogFormatter.init();
// Parse command line arguments
OptionParser parser = new OptionParser();
OptionSet opts = null;
List&lt;String&gt; nonOpts = null;
try {
    parser.accepts("localhost", "Connect to the localhost node");
    parser.accepts("help", "Displays program options");
    opts = parser.parse(args);
    if (opts.has("help")) {
        System.out.println("usage: org.bitcoinj.examples.FetchBlock [--localhost] &lt;blockHash&gt;");
        parser.printHelpOn(System.out);
        return;
    }
    nonOpts = opts.nonOptionArguments();
    if (nonOpts.size() != 1) {
        throw new IllegalArgumentException("Incorrect number of block hash, please provide only one block hash.");
    }
} catch (OptionException | IllegalArgumentException e) {
    System.err.println(e.getMessage());
    System.err.println("usage: org.bitcoinj.examples.FetchBlock [--localhost] &lt;blockHash&gt;");
    parser.printHelpOn(System.err);
    return;
}

// Connect to testnet and find a peer
System.out.println("Connecting to node");
final NetworkParameters params = TestNet3Params.get();
BlockStore blockStore = new MemoryBlockStore(params);
BlockChain chain = new BlockChain(params, blockStore);
PeerGroup peerGroup = new PeerGroup(params, chain);
if (!opts.has("localhost")) {
    peerGroup.addPeerDiscovery(new DnsDiscovery(params));
} else {
    PeerAddress addr = new PeerAddress(params, InetAddress.getLocalHost());
    peerGroup.addAddress(addr);
}
peerGroup.start();
peerGroup.waitForPeers(1).get();
Peer peer = peerGroup.getConnectedPeers().get(0);

// Retrieve a block through a peer
Sha256Hash blockHash = Sha256Hash.wrap(nonOpts.get(0));
Future&lt;Block&gt; future = peer.getBlock(blockHash);
System.out.println("Waiting for node to send us the requested block: " + blockHash);
Block block = future.get();
System.out.println(block);
peerGroup.stopAsync();
</code></pre>

CC BY-SA 4.0
2018-05-26T07:46:08.327Z
Please add the details of the link as it relates to the question in your answer. In the current answer, if the link ceases to work, future users will not be able to get any information from it.
Raghav Sood
CC BY-SA 4.0
2018-05-25T18:11:47.533Z
2018-06-02T13:06:28.697Z
2018-06-02T13:06:28.697Z
Nick
<p>I would like to download a full block using Java and bitcoinj and then parse it to an array to be able to work with it. Here is some (very basic) pseudocode:</p>

<pre><code>connect to network;  
download latestBlock;  
write/convert block to readable array;  
read blockArray;  
   sout: blockArray[23][55] // this should get me eg. a txid, address, ...  
// or similar. I just want to be able to read values from the block 
</code></pre>

<p>Is there a way to do this? I found the following solution here (<a href="https://bitcoin.stackexchange.com/questions/21182/using-bitcoinj-to-discover-peers-and-download-the-block-chain">Using bitcoinj to discover peers and download the block chain</a>):</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>

<p>Unfortunately, kit.startAndWait() isn't available anymore.</p>

<p>Can someone help me to get the latest block and be able to read its values?</p>

<p>Thank you very much :)</p>

<bitcoinj>
Java: bitcoinj; how to download a full block?
A.c
# Java: bitcoinj; how to download a full block?

(Asked by: username_0 on 2017-12-02)

I would like to download a full block using Java and bitcoinj and then parse it to an array to be able to work with it. Here is some (very basic) pseudocode:
connect to network;  
download latestBlock;  
write/convert block to readable array;  
read blockArray;  
   sout: blockArray[23][55] // this should get me eg. a txid, address, ...  
// or similar. I just want to be able to read values from the block 

Is there a way to do this? I found the following solution here (Using bitcoinj to discover peers and download the block chain):
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);
    }
}

Unfortunately, kit.startAndWait() isn't available anymore.
Can someone help me to get the latest block and be able to read its values?
Thank you very much :)

**Tags:** bitcoinj


---

## 1 answers

### Answer by username_1 on 2018-05-25. Score: 1

Example in bitcoinj source code is exactly what you are looking for:
BriefLogFormatter.init();
// Parse command line arguments
OptionParser parser = new OptionParser();
OptionSet opts = null;
List nonOpts = null;
try {
    parser.accepts("localhost", "Connect to the localhost node");
    parser.accepts("help", "Displays program options");
    opts = parser.parse(args);
    if (opts.has("help")) {
        System.out.println("usage: org.bitcoinj.examples.FetchBlock [--localhost] ");
        parser.printHelpOn(System.out);
        return;
    }
    nonOpts = opts.nonOptionArguments();
    if (nonOpts.size() != 1) {
        throw new IllegalArgumentException("Incorrect number of block hash, please provide only one block hash.");
    }
} catch (OptionException | IllegalArgumentException e) {
    System.err.println(e.getMessage());
    System.err.println("usage: org.bitcoinj.examples.FetchBlock [--localhost] ");
    parser.printHelpOn(System.err);
    return;
}

// Connect to testnet and find a peer
System.out.println("Connecting to node");
final NetworkParameters params = TestNet3Params.get();
BlockStore blockStore = new MemoryBlockStore(params);
BlockChain chain = new BlockChain(params, blockStore);
PeerGroup peerGroup = new PeerGroup(params, chain);
if (!opts.has("localhost")) {
    peerGroup.addPeerDiscovery(new DnsDiscovery(params));
} else {
    PeerAddress addr = new PeerAddress(params, InetAddress.getLocalHost());
    peerGroup.addAddress(addr);
}
peerGroup.start();
peerGroup.waitForPeers(1).get();
Peer peer = peerGroup.getConnectedPeers().get(0);

// Retrieve a block through a peer
Sha256Hash blockHash = Sha256Hash.wrap(nonOpts.get(0));
Future future = peer.getBlock(blockHash);
System.out.println("Waiting for node to send us the requested block: " + blockHash);
Block block = future.get();
System.out.println(block);
peerGroup.stopAsync();

> **Comments:**
> **username_2**: Please add the details of the link as it relates to the question in your answer. In the current answer, if the link ceases to work, future users will not be able to get any information from it.  

---

