随着区块链技术的飞速发展,以太坊作为最智能的平台,已经不仅仅是加密货币的载体,更成为了构建去中心化应用(Dapp)的基石,对于庞大的Java开发者群体而言,如何利用熟悉的Java语言与以太坊网络进行交互,成为一个极具吸引力的课题,本文将深入探讨如何使用Java调用以太坊,涵盖核心概念、主流工具、代码示例以及实践中的注意事项,为开发者铺就一条从理论到实践的道路。
在众多编程语言中,Java以其跨平台性、稳定性和成熟的生态系统脱颖而出,选择Java与以太坊交互,主要有以下优势:
在深入代码之前,我们必须理解几个核心概念,它们是Java与以太坊通信的基石。

在开始编码前,我们需要准备开发环境。
下面我们通过代码示例,展示Java如何通过Web3j执行最常见的以太坊操作。
添加Web3j依赖

在你的pom.xml(Maven)或build.gradle(Gradle)文件中添加Web3j依赖:
<!-- Maven -->
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.9.8</version> <!-- 请使用最新版本 -->
</dependency>
连接到以太坊节点
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
public class Web3jConnection {
public static void main(String[] args) {
// 替换为你的节点URL (Infura 或本地节点)
String nodeUrl = "https://mainnet.infura.io/v3/YOUR_PROJECT_ID";
// 创建Web3j实例
Web3j web3j = Web3j.build(new HttpService(nodeUrl));
try {
// 测试连接
String clientVersion = web3j.web3ClientVersion().send().getWeb3ClientVersion();
System.out.println("成功连接到以太坊节点,客户端版本: " clientVersion);
} catch (Exception e) {
System.err.println("连接失败: " e.getMessage());
}
}
}
读取账户余额

import org.web3j.protocol.core.methods.response.EthGetBalance;
import java.math.BigInteger;
public class GetBalance {
public static void main(String[] args) {
String nodeUrl = "https://mainnet.infura.io/v3/YOUR_PROJECT_ID";
Web3j web3j = Web3j.build(new HttpService(nodeUrl));
// 替换为你要查询的地址
String address = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
try {
EthGetBalance balance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send();
BigInteger weiValue = balance.getBalance();
// 将Wei转换为Ether (1 Ether = 10^18 Wei)
double etherValue = weiValue.doubleValue() / Math.pow(10, 18);
System.out.println("地址 " address " 的余额是: " etherValue " ETH");
} catch (Exception e) {
System.err.println("获取余额失败: " e.getMessage());
}
}
}
发送交易(转账ETH)
发送交易比查询复杂,因为它需要签名和支付Gas费。
import org.web3j.crypto.Credentials;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.utils.Convert;
import org.web3j.utils.Numeric;
import java.math.BigInteger;
public class SendTransaction {
public static void main(String[] args) throws Exception {
String nodeUrl = "https://mainnet.infura.io/v3/YOUR_PROJECT_ID";
Web3j web3j = Web3j.build(new HttpService(nodeUrl));
// 1. 准备凭证:从私钥创建
// !!! 警告:此处的私钥仅用于示例,实际使用中必须从安全的地方加载,如环境变量或密钥库文件 !!!
String privateKey = "YOUR_PRIVATE_KEY"; // 发送方私钥
Credentials credentials = Credentials.create(privateKey);
// 2. 准备交易参数
String toAddress = "0xRecipientAddressHere";
BigInteger value = Convert.toWei("0.01", Convert.Unit.ETHER).toBigInteger(); // 转账0.01 ETH
BigInteger gasLimit = BigInteger.valueOf(21000); // 转账ETH的典型Gas Limit
BigInteger gasPrice = web3j.ethGasPrice().send().getGasPrice(); // 获取当前建议的Gas Price
// 3. 创建原始交易
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
credentials.getAddress(), // nonce由web3j自动处理
gasPrice,
gasLimit,
toAddress,
value
);
// 4. 签名交易
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
// 5. 发送交易
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send();
if (ethSendTransaction.getTransactionHash() != null) {
System.out.println("交易已发送,交易哈希: " ethSendTransaction.getTransactionHash());
} else {
System.err.println("交易发送失败: " ethSendTransaction.getError().getMessage());
}
}
}
与智能合约交互
这是Java调用以太坊最强大的功能,假设我们有一个简单的SimpleStorage合约,它有一个store(uint256)函数和一个get()函数。
import org.web3j.abi.Function; import org.web3j.abi.TypeReference; import org.web3j.abi.datatypes.Address; import org.web3j.abi.datatypes.Bool; import org.web3j.abi.datatypes.Type; import org.web3j.abi.datatypes.Utf8String; import org.web3j.abi.datatypes.generated.Uint256; import org.web3j.protocol.core.methods.response.EthCall; import org.web3j.protocol.core.methods.response.TransactionReceipt; import org.web3j.tx.Contract; import org.web3j.tx.gas.ContractGas
免责声明:本文为转载,非本网原创内容,不代表本网观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
如有疑问请发送邮件至:bangqikeconnect@gmail.com