:2026-04-01 22:06 点击:6
在区块链技术快速发展的今天,以太坊作为最智能合约平台之一,吸引了大量开发者的关注,对于Java生态的开发者而言,如何与以太坊网络交互(如部署合约、调用方法、查询交易等)是一个核心问题。Web3j应运而生——它是一个轻量级、模块化的Java库,专门用于与以太坊节点通信,支持以太坊的所有核心功能(JSON-RPC API封装、合约交互、钱包管理等),本文将详细介绍Web3j的安装、配置及核心功能使用,帮助Java开发者快速上手以太坊开发。
Web3j是100%用Java编写的以太坊交互库,具有以下优势:
在使用Web3j之前,需完成以下环境配置:
Web3j基于Java 8+开发,需安装JDK(建议JDK 11或更高版本)并配置JAVA_HOME环境变量,可通过以下命令检查Java版本:
java -version
若使用Maven或Gradle管理项目,需提前安装对应工具,本文以Maven为例,在pom.xml中添加Web3j依赖:
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.9.8</version> <!-- 建议使用最新版本 -->
</dependency>
<!-- 支持Solidity合约编译为Java类 -->
<dependency>
<groupId>org.web3j</groupId>
<artifactId>solidity</artifactId>
<version>4.9.8</version>
</dependency>
Web3j需要连接到以太坊节点才能交互,开发者可选择以下方式获取节点:
geth --http --http.addr "0.0.0.0" --http.port "8545" --http.api "eth,web3,personal"
Web3j通过Web3j类建立与节点的连接,以下是连接示例:
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
public class Web3jConnection {
public static void main(String[] args) {
// 连接本地节点(默认HTTP端口8545)
Web3j web3j = Web3j.build(new HttpService("http://localhost:8545"));
// 测试连接是否成功
try {
String clientVersion = web3j.web3ClientVersion().send().getClientVersion();
System.out.println("连接成功,客户端版本:" + clientVersion);
} catch (Exception e) {
System.err.println("连接失败:" + e.getMessage());
}
}
}
Web3j web3j = Web3j.build(new HttpService("https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"));
import org.web3j.protocol.core.methods.response.PersonalNewAccount;
public class AccountManagement {
public static void main(String[] args) throws Exception {
Web3j web3j = Web3j.build(new HttpService("http://localhost:8545"));
// 创建新账户(需节点开启personal API)
PersonalNewAccount account = web3j.personalNewAccount("your_password").send();
System.out.println("新账户地址:" + account.getAccount());
}
}
// 解锁账户(单位:秒,0表示永久解锁)
web3j.personalUnlockAccount("account_address", "password", 60).s
end();
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.utils.Convert;
import org.web3j.utils.Convert.Unit;
public class TransferEther {
public static void main(String[] args) throws Exception {
Web3j web3j = Web3j.build(new HttpService("http://localhost:8545"));
String fromAddress = "0xYourFromAddress";
String toAddress = "0xYourToAddress";
String password = "your_password";
// 1. 解锁账户
web3j.personalUnlockAccount(fromAddress, password, 60).send();
// 2. 构建交易参数
Convert.Unit unit = Convert.Unit.ETHER;
BigInteger value = Convert.toWei("0.1", unit).toBigInteger(); // 转账0.1 ETH
BigInteger gasLimit = BigInteger.valueOf(21000); // 标准转账Gas限制
BigInteger gasPrice = web3j.ethGasPrice().send().getGasPrice(); // 获取当前Gas价格
// 3. 发送交易
EthSendTransaction transaction = web3j.ethSendTransaction(
org.web3j.protocol.core.methods.Transaction.createEtherTransaction(
fromAddress, null, gasPrice, gasLimit, toAddress, value
)
).send();
System.out.println("交易哈希:" + transaction.getTransactionHash());
}
}
假设有一个简单的SimpleStorage.sol合约:
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private storedData;
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint256) {
return storedData;
}
}
编译步骤:
SolidityFunctionWrapperGenerator生成Java封装类: web3j solidity generate -a SimpleStorage.abi -b SimpleStorage.bin -p com.example.contract
执行后会在com.example.contract包下生成SimpleStorage.java类。
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.tx.Contract;
import org.web3j.tx.gas.DefaultGasProvider;
public class ContractDeploy {
public static void main(String[] args) throws Exception {
Web3j web3j = Web3j.build(new HttpService("http://localhost:8545"));
// 加载账户和私钥(实际项目中应从安全存储中读取)
String privateKey = "your_private_key";
Credentials credentials = Credentials.create(privateKey);
// 部署合约(需提供合约字节码和构造函数参数)
SimpleStorage contract = SimpleStorage.deploy(
web3j,
credentials,
DefaultGasProvider.GAS_PRICE,
DefaultGasProvider.GAS_LIMIT,
BigInteger.ZERO // 构造函数参数(无)
).send();
System.out.println("合约地址:" + contract.getContractAddress());
}
}
public class ContractInteraction {
public static void main(String[] args) throws Exception {
Web3j web3j = Web3j.build(new HttpService("http://localhost:8545"));
String privateKey = "your_private_key";
Credentials credentials = Credentials.create(privateKey);
// 加载已部署的合约
String contractAddress = "0xYourContractAddress";
SimpleStorage contract = SimpleStorage.load(
contractAddress,
web3j,
credentials,
DefaultGasProvider.GAS_PRICE,
DefaultGasProvider.GAS_LIMIT
);
// 调用set方法(修改状态,需要发送交易)
EthSendTransaction tx = contract.set(42).send();
System.out.println("交易哈希:" + tx.getTransactionHash());
// 调
本文由用户投稿上传,若侵权请提供版权资料并联系删除!