主页 > imtoken安卓官网 > eclipse gradle jar包_Web3j以太坊开发环境搭建教程【Mav】

eclipse gradle jar包_Web3j以太坊开发环境搭建教程【Mav】

imtoken安卓官网 2023-08-10 05:12:13

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

使用你熟悉的语言学习以太坊DApp开发:Java | PHP | 蟒蛇 | .Net/C# | 戈朗 | 节点JS | 颤振/飞镖

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,你可以看看这个教程:Developing web3j Ethereum applications with Gradle in Eclipse。

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

2.新建一个Maven项目

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

7c3fc9a2cdcb593929cdf9e87f5bf7eb.png

项目浏览器中应出现以下内容:

12340c634aa9efdc3cec9e70fdda9b23.png

最后我们还需要告诉 Eclipse 和 Maven 使用 Java 8。编辑 pom.xml 文件并添加

前:


  1.8
  1.8

在项目浏览器中右击项目名称,选择Maven > Update Project,在弹出的对话框中点击OK。 您应该看到项目浏览器中的 JER 系统库从 JavaSE-1.5 更改为 JavaSE-1.8:

a1b8effd7f83ab17ed581382a2b7c86b.png

3.将web3j库添加到项目中

以太坊eth2.0代币开发_以太坊web开发_可以在以太坊上开发什么

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

在 Eclipse 中编辑文件 pom.xml 并放入

在前面添加以下内容:


  
    org.web3j
    core
    4.3.0
  

可以在此处找到完整的 pom.xml 文件。

保存上述文件后以太坊web开发,会导入声明的依赖包。 在包浏览器中,您会看到一个 Maven 依赖文件夹,其中包含 web3j 等 JAR 包。

4.创建主类

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

右键单击该项目并选择新建 > 类以创建 Java 类 Main.java。 输入包名io.kauri.tutorials.java_ethereum,类名Main以太坊web开发,选择public static void main(String[] args):

d5858c40d36534d949c4d3bab8e1fa11.png

单击“完成”以生成文件。

//Main.java
package io.kauri.tutorials.java_ethereum;
public class Main {

以太坊web开发_以太坊eth2.0代币开发_可以在以太坊上开发什么

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 端点默认为:8545:

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

如果您在自己的机器上运行图形 Ganache 应用程序,您的 JSON-RPC API 端点将默认为:7545:

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

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

如果您使用的是 Infura,Node 的 JSON-RPC API 端点是 .infura.io/v3/

以太坊eth2.0代币开发_可以在以太坊上开发什么_以太坊web开发

:

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);
}

注意:IOException可能出现在JSON-RPC请求的序列化中,需要处理。

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

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

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

以太坊web开发_可以在以太坊上开发什么_以太坊eth2.0代币开发

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.

以太坊eth2.0代币开发_可以在以太坊上开发什么_以太坊web开发

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

截图如下:

4ce85bb4d448e81e4dc8b6db6be46d66.png

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