:2026-03-06 20:57 点击:2
以太坊作为全球领先的智能合约平台,其生态系统拥有丰富的开发工具和库,对于Java开发者而言,虽然没有像Go或Python那样原生的官方客户端(如geth或parity),但依然有多个优秀的Java库和客户端实现,可以方便地与以太坊网络进行交互,本文将介绍以太坊Java客户端的选择、环境搭建、基本功能使用以及一个简单的实践案例,帮助Java开发者快速上手以太坊开发。
在Java生态中,与以太坊交互主要通过以下几种方式:
ethereumj(一个用Java实现的完整以太坊节点,但活跃度相对较低)等,但Web3j仍然是社区首选。对于大多数应用场景,尤其是需要快速集成以太坊功能的应用,Web3j是最佳选择,本文将重点介绍Web3j的使用。
JAVA_HOME。以Maven为例,在pom.xml中添加Web3j依赖:
<dependencies>
<!-- Web3j Core -->
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.9.8</version> <!-- 请使用最新版本 -->
</dependency>
<!-- 其他可选依赖,如用于Solidity代码生成的web3j-so
lidity -->
<dependency>
<groupId>org.web3j</groupId>
<artifactId>solidity</artifactId>
<version>4.9.8</version> <!-- 与core版本保持一致 -->
</dependency>
</dependencies>
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import java.io.IOException;
public class Web3jConnection {
public static void main(String[] args) {
// 替换为你的Infura URL 或 本地节点URL
String infuraUrl = "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID";
// 本地节点示例: "http://localhost:8545"
Web3j web3j = Web3j.build(new HttpService(infuraUrl));
try {
// 检查连接是否成功,获取最新区块号
String latestBlockNumber = web3j.ethBlockNumber().send().getBlockNumber().toString();
System.out.println("连接成功!最新区块号: " + latestBlockNumber);
} catch (IOException e) {
System.err.println("连接以太坊节点失败: " + e.getMessage());
e.printStackTrace();
} finally {
// 关闭连接
web3j.shutdown();
}
}
}
获取账户余额
import org.web3j.protocol.core.methods.response.EthGetBalance;
import java.math.BigInteger;
public class GetBalance {
public static void main(String[] args) throws IOException {
Web3j web3j = Web3j.build(new HttpService("https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"));
String address = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"; // 替换为要查询的地址
EthGetBalance balance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send();
BigInteger weiBalance = balance.getBalance();
// 将Wei转换为Ether (1 Ether = 10^18 Wei)
String etherBalance = Convert.fromWei(weiBalance.toString(), Convert.Unit.ETHER).toPlainString();
System.out.println("地址: " + address);
System.out.println("余额: " + etherBalance + " ETH");
web3j.shutdown();
}
}
发送交易(以太转账)
发送交易需要私钥签名,通常建议使用Credentials类,并妥善保管私钥。
import org.web3j.crypto.Credentials;
import org.web3j.crypto.TransactionUtils;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Transfer;
import org.web3j.utils.Convert;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.ExecutionException;
public class SendTransaction {
public static void main(String[] args) throws Exception {
Web3j web3j = Web3j.build(new HttpService("https://sepolia.infura.io/v3/YOUR_INFURA_PROJECT_ID")); // 使用测试网
// 替换为你的接收方地址和私钥(测试网私钥,不要用主网真私钥!)
String recipientAddress = "0xReCiPiEnTaDrEsS...";
String privateKey = "YOUR_PRIVATE_KEY"; // 测试网私钥
Credentials credentials = Credentials.create(privateKey);
// 使用Transfer类发送以太(简单方式)
// 注意:测试网需要一些测试ETH才能发送
try {
Transfer.sendFunds(
web3j,
credentials,
recipientAddress,
BigDecimal.valueOf(0.001), // 转账数量(ETH)
Convert.Unit.ETHER
).send();
System.out.println("交易发送成功!");
// 或者手动构建并发送交易(更灵活)
/*
BigInteger value = Convert.toWei("0.001", Convert.Unit.ETHER).toBigInteger();
EthSendTransaction transaction = web3j.ethTransfer(credentials, recipientAddress, value)
.sendAsync()
.get();
String transactionHash = transaction.getTransactionHash();
System.out.println("交易发送成功,交易哈希: " + transactionHash);
// 等待交易被打包
TransactionReceipt receipt = transaction.getTransactionReceipt().orElseThrow(() -> new RuntimeException("交易未获取到回执"));
System.out.println("交易回执: " + receipt.toString());
*/
} catch (Exception e) {
System.err.println("交易发送失败: " + e.getMessage());
e.printStackTrace();
}
web3j.shutdown();
}
}
智能合约交互
智能合约交互是Web3j的核心功能之一。
solc编译器或在线编译器(如Remix IDE)得到ABI和字节码。web3j generate solidity -a /path/to/your/contract.abi -b /path/to/your/contract.bin -o /path/to/output/dir -p com.your.package
import org.web3j.tx.Contract;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.StaticGasProvider;
import java.math.BigInteger;
public class DeployContract {
public static void main(String[] args) throws Exception {
Web3j web3j = Web3j.build(new HttpService("https://sepolia.infura.io/v3/YOUR_INFURA_PROJECT_ID"));
String privateKey = "YOUR_PRIVATE_KEY";
Credentials credentials = Credentials.create(privateKey);
// 假设你已经生成了Your
本文由用户投稿上传,若侵权请提供版权资料并联系删除!