Connect to Ethereum [parity / Maven / eclipse] via web3j interface

boy918. 2022-04-06 01:17:04 阅读数:188

connect ethereum parity maven eclipse


One 、Java development environment

Two 、Maven Project creation

3、 ... and 、 take web3j Add item to Library

One 、Java development environment

1.JDK download

Refer to my last article JDK and eclipse Environment configuration of , After all are set up, go to the following steps .

Two 、Maven Project creation

1.Maven Project creation

      1) start-up Eclipse, Click on File > New > Project > Maven > Maven Project > Next

 2) Choose Create a simple project ( skip archetype selection) And then click Next

 3) Enter the Group ID and Artifact ID, And then click Finish.

Group Id:
Artifact Id: java-ethereum

4) After completion, the following interface is displayed

 5)Eclipse and Maven And use Java Matching version . I am here java8, Need to edit pom.xml Document and </project> Before adding , Then save :


6) Right click on the project name and select Maven > Update Project, Click in the pop-up dialog box OK. See... In the project browser JER System library from JavaSE-1.5 Turned into JavaSE-1.8:

3、 ... and 、 take web3j Add item to Library

1) take web3j The latest version of maven Import the project , stay Eclipse Edit file pom.xml And in </project> Add the following before :


After saving the above file, the declared dependent package will be automatically imported , Then you will see a in the package browser Maven The dependent folder contains web3j etc. JAR package .

 2) establish Main class : Right click on the item and select New > Class, Enter the package name io.kauri.tutorials.java_ethereum、 Class name Main, And select public static void main(String[] args), Last point finish:

3) Use web3j Connect the Ethereum node , The following is the complete Ethereum blockchain Java Access code

package io.kauri.tutorials.java_ethereum;
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;
// The above is the package required by the code
public class Main {
public static void main(String[] args) {
System.out.println("Connecting to Ethereum ...");
Web3j web3 = HttpService("http://localhost:8540"));
// To connect Ethereum nodes ,Web3j need JSON-RPC API Access end node ,http://localhost:8540 According to their own Ethereum interface input ,8540 I built it parit Interface end of
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);
//JSON-RPC Serialization of requests may occur IOException abnormal , So we need to deal with .

 4) Run locally Geth、Parity、Pantheon or Ganache-cli, And keep the nodes connected , See document for details :Linux Next parity The realization of alliance chain _boy918. The blog of -CSDN Blog

 5) Right click on the file, And select Run As > Java Application function Java Program , You may encounter this error for the first time , Need to be in pom.xml Add dependency

6) stay pom.xml Add dependencies and save , Rerun the project , See the following contents displayed on the console


       The above is Web3j The interface is connected to Ethereum The whole process . In case of doubt , Please confide in me .