以太坊Web3j/Maven/Eclipse开发环境搭建教程

在这个教程中,我们将学习如何在Eclipse中创建一个采用Maven管理的Java以太坊项目,使用web3j库连接以太坊节点,执行JSON-RPC API调用并显示结果。

用自己熟悉的语言学习以太坊DApp开发:Java | Php | Python | .Net / C# | Golang | Node.JS | Flutter / Dart

web3j是一个轻量级的、模块化的开发库,它实现了与以太坊交互所需 的全部功能,包括JSON-RPC API客户端、钱包账号管理、Java智能合约封装器、对ENS、ERC20、ERC721等特性的支持等等。

1、准备Java以太坊开发环境

首先需要安装Java 8。使用如下命令验证java的安装情况:

$ java -version
java version "1.8.0_201"

其次我们需要一个包管理器,例如Maven或Gradle。在这个教程中我们使用Maven管理依赖关系,如果你希望使用Gradle,可以查看这个教程:在Eclipse中使用Gradle开发web3j以太坊应用。

最后,我们需要一个集成开发环境,例如在这个教程中使用的Eclipse。

2、创建一个新的Maven项目

在这一环境,我们要完成的任务是,在Eclipse中创建一个新的Maven项目,并将其命名为java_ethereum

以太坊Web3j/Maven/Eclipse开发环境搭建教程_第1张图片

  • 启动Eclipse之后,前往File > New > Project > Maven > Maven Project菜单。
  • 选中Create a simple project (跳过 archetype selection)然后点击 Next >
  • 输入项目的Group ID 和Artifact ID,然后点击Finish。
    • Group Id: io.kauri.tutorials.java-ethereum
    • Artifact Id: java-ethereum

在项目浏览器中应当显示如下内容:

以太坊Web3j/Maven/Eclipse开发环境搭建教程_第2张图片
最后我们还需要告诉Eclipse和Maven使用Java 8。编辑pom.xml文件并在之前:


  1.8
  1.8

在项目浏览器中右键点击项目名并选择 Maven > Update Project,在弹出的对话框中点击OK。你应当看到项目浏览器中的JER系统库从JavaSE-1.5变成了JavaSE-1.8:

以太坊Web3j/Maven/Eclipse开发环境搭建教程_第3张图片

3、将web3j库加入项目

在这一步,我们将web3j的最新版本通过maven导入项目。

在Eclipse中编辑文件pom.xml并在之前添加以下内容:


  
    org.web3j
    core
    4.3.0
  

完整的pom.xml文件参见这里。

保存上述文件后就会导入声明的依赖包。在包浏览器中会看到一个Maven依赖文件夹其中包含了web3j等JAR包。

4、创建Main类

现在有了使用Web3j所需的全部依赖项,我们可以开始编写以太坊Java程序了。

右键点击项目并选择New > Class来创建一个Java类Main.java。输入包名
io.kauri.tutorials.java_ethereum、类名Main 并选中public static void main(String[] args)

以太坊Web3j/Maven/Eclipse开发环境搭建教程_第4张图片

点击Finish来生成文件。

//Main.java
package io.kauri.tutorials.java_ethereum;

public class Main {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
  }
}

5、使用web3j连接以太坊节点

我们已经创建了项目,导入了Web3j库并开始准备实现代码。现在我们可以连接一个以太坊节点并利用Web3j的JSON-RPC API抽象来执行一些以太坊操作。

首先引入代码需要的包,或者允许你的IDE自动导入:

import java.io.IOException;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;

要连接以太坊节点,Web3j需要JSON-RPC API访问端结点:

Web3j web3 = Web3j.build(new HttpService(""));

如果你是本地运行的Geth、Parity、Pantheon或Ganache-cli,那么你的节点的JSON-RPC API端结点默认就是http://localhost:8545

Web3j web3 = Web3j.build(new HttpService("http://localhost:8545"));

如果你在自己的机器上运行图形版Ganache应用,那么你的JSON-RPC API的端结点默认就是http://localhost:7545

Web3j web3 = Web3j.build(new HttpService("http://localhost:7545"));

注意:作为一个测试链,Ganache不是所有的JSON-RPC API都支持,例如net_peercount。

如果你使用的是Infura,那么节点的JSON-RPC API端结点是https://.infura.io/v3/

Web3j web3 = Web3j.build(new HttpService("https://mainnet.infura.io/v3/"));

Web3j实现了以太坊的JSON-RPC API客户端,其使用方式为 = web3..send()。例如:

try {
  // web3_clientVersion returns the current client version.
  Web3ClientVersion clientVersion = web3.web3ClientVersion().send();

  //eth_blockNumber returns the number of most recent block.
  EthBlockNumber blockNumber = web3.ethBlockNumber().send();

  //eth_gasPrice, returns the current price per gas in wei.
  EthGasPrice gasPrice =  web3.ethGasPrice().send();

} catch(IOException ex) {
  throw new RuntimeException("Error whilst sending json-rpc requests", ex);
}

注意:JSON-RPC请求的序列化可能会出现IOException异常,因此需要处理一下。

6、完整的以太坊区块链Java访问代码

下面的代码展示了连接以太坊节点并执行JSON-RPC API调用的完整的Java程序代码。

//Main.java
package io.kauri.tutorials.java_ethereum;

import java.io.IOException;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;

public class Main {

  public static void main(String[] args) {
    System.out.println("Connecting to Ethereum ...");
    Web3j web3 = Web3j.build(new HttpService("http://localhost:8545"));
    System.out.println("Successfuly connected to Ethereum");

    try {
      // web3_clientVersion returns the current client version.
      Web3ClientVersion clientVersion = web3.web3ClientVersion().send();

      // eth_blockNumber returns the number of most recent block.
      EthBlockNumber blockNumber = web3.ethBlockNumber().send();

      // eth_gasPrice, returns the current price per gas in wei.
      EthGasPrice gasPrice = web3.ethGasPrice().send();

      // Print result
      System.out.println("Client version: " + clientVersion.getWeb3ClientVersion());
      System.out.println("Block number: " + blockNumber.getBlockNumber());
      System.out.println("Gas price: " + gasPrice.getGasPrice());

    } catch (IOException ex) {
      throw new RuntimeException("Error whilst sending json-rpc requests", ex);
    }
  }
}

右键点击文件Main.java并选择 Run As > Java Application就可以运行这个Java程序了。你应该能看到控制台显示如下内容;

Connecting to Ethereum ...
Successfuly connected to Ethereum
Client version: Geth/v1.8.22-omnibus-260f7fbd/linux-amd64/go1.11.1
Block number: 7983049
Gas price: 3000000000

截屏如下:

以太坊Web3j/Maven/Eclipse开发环境搭建教程_第5张图片


原文链接:Java以太坊开发环境搭建 — 汇智网

你可能感兴趣的:(以太坊开发)