title: fabric-sdk-java How to implement the Network.addBlockListener method in my App.java
body: <p>I am new to Hyperlegder-Fabric and already manage to connect to a network through the org.hyperledger.fabric.gateway package.</p>
  <p>This is my App.java class:</p>
  <pre><code>
  //Fabric Imports
  import org.hyperledger.fabric.gateway.*;
  import org.hyperledger.fabric.sdk.BlockEvent;
  import org.hyperledger.fabric.sdk.ChaincodeEventListener;
  
  //Other Imports
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.nio.charset.StandardCharsets;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  import java.security.InvalidKeyException;
  import java.security.PrivateKey;
  import java.security.cert.CertificateException;
  import java.security.cert.CertificateFactory;
  import java.security.cert.X509Certificate;
  import java.util.concurrent.TimeoutException;
  import java.util.function.Consumer;
  
  
  public class App{
  
  public static void main(String[] args) throws IOException {
          // Load an existing wallet holding identities used to access the network.
          Path walletDirectory = Paths.get(&quot;wallet&quot;);
          Wallet wallet = Wallets.newFileSystemWallet(walletDirectory);
  
          // Path to a common connection profile describing the network.
          Path networkConfigFile = Paths.get(&quot;connection.json&quot;);
  
          // Configure the gateway connection used to access the network.
          Gateway.Builder builder = Gateway.createBuilder()
                  .identity(wallet, &quot;user1&quot;)
                  .networkConfig(networkConfigFile);
  
          // Create a gateway connection
          try (Gateway gateway = builder.connect()) {
  
              // Obtain a smart contract deployed on the network.
              Network network = gateway.getNetwork(&quot;mychannel&quot;);
              Contract contract = network.getContract(&quot;fabcar&quot;);
  
              // Submit transactions that store state to the ledger.
              byte[] createCarResult = contract.createTransaction(&quot;createCar&quot;)
                      .submit(&quot;CAR10&quot;, &quot;VW&quot;, &quot;Polo&quot;, &quot;Grey&quot;, &quot;Mary&quot;);
              System.out.println(new String(createCarResult, StandardCharsets.UTF_8));
  
              // Evaluate transactions that query state from the ledger.
              byte[] queryAllCarsResult = contract.evaluateTransaction(&quot;queryAllCars&quot;);
              System.out.println(new String(queryAllCarsResult, StandardCharsets.UTF_8));
  
          } catch (ContractException | TimeoutException | InterruptedException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  <p>Now I need to listen for block events, so I see in the documentation that the Network package contains the <strong>addBlockListener</strong> method (<a href="https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Network.html" rel="nofollow noreferrer">https://hyperledger.github.io/fabric-gateway-java/release-2.2/org/hyperledger/fabric/gateway/Network.html</a>)</p>
  <p>My doubt is how i can implement this method in the above App.java file so i can get the block number, etc. I am not a java developer i am struggling a lot on this since.</p>
  <p>Appreciate any help.</p>