Connecting to an Ethereum client with Java, Eclipse and Web3j
Ethereum is a Blockchain, which means it operates on a peer-to-peer network composed of thousand of nodes where each node agrees on the next state.
In order to interact with the Ethereum global state (distributed database), a program needs to connect to a node that exposes the standard JSON-RPC API which can be used to execute operations on the Ethereum blockchain.
In this article, we will learn how to start an Ethereum Java project and connect to a node using the Java library Web3j, a lightweight and modular library implementing all the functionallities required to work with Ethereum (JSON-RPC API client, wallet account management, Java Smart Contract wrapper, support for ENS, ERC20 and ERC721 and much more).
Prerequisite
To run this tutorial, we must have the following installed:
1 | java -version |
- A package and dependancy manager, for example Maven or Gradle
- An IDE (Integrated development environment), for this tutorial, we use Eclipse
Start a new project
First create a new Maven project called java_ethereum
in Eclipse.
1. Create a new Maven project
Once Eclipse is launched, we need to create a new Maven project. Go to File > New > Project > Maven > Maven Project
Check the box Create a simple project (skip archetype selection) and click on Next >.
Next screen, enter the Group ID and Artifact ID of our project then click Finish.
Group Id: io.kauri.tutorials.java-ethereum
Artifact Id: java-ethereum
It should result of a new project in the Project Explorer
2. Configure our project to use Java 8
Finally, we need to tell Eclipse and Maven to use Java version 8.
Edit the file pom.xml
and add the following lines before </project>
1 | <properties> |
Now, right click on the project name in the Project Explorer and click on Maven > Update Project. Click OK in the dialog box that pops up.
In the Project Explorer, You should see the JRE System library changing from JavaSE-1.5 to JavaSE-1.8.
Add Web3j library to our project
In this step, we import the latest version of Web3j to our project via maven.
In Eclipse, edit the file pom.xml
and add the following lines before </project>
:
1 | <dependencies> |
Full pom.xml file available here
Save file and dependencies will import. In your package explorer you will see a Maven dependencies folder with all the JAR (Java ARchive) packages for web3j and its dependencies.
Create a Main class
Now, we have all the required dependencies to use Web3j, we can start coding our Ethereum Java program.
Create a Java class Main.java
in your project by right-clicking on the project and selecting New > Class.
Enter the package name io.kauri.tutorials.java_ethereum
, the class name Main
and check public static void main(String[] args).
Click on Finish to generate the skeleton file.
1 | //Main.java |
Connect to an Ethereum node with Web3j.
Now we have created our project, imported the Web3j library and prepared a program to run our code. We can now connect to an Ethereum node and start executing operations over the JSON-RPC API abstracted by Web3j.
1. Add imports
First import the packages needed for our code, or allow your IDE to automatically import them for you:
1 | import java.io.IOException; |
2. Connect to the node
To connect to the node, Web3j requires the JSON-RPC API endpoint:
1 | Web3j web3 = Web3j.build(new HttpService("<NODE ENDPOINT>")); |
Local Ethereum node or ganache-cli
If you are running locally a Geth, Parity, Pantheon client or ganache-cli. Your node JSON-RPC API endpoint is http://localhost:8545
by default
1 | Web3j web3 = Web3j.build(new HttpService("http://localhost:8545")); |
Ganache application: Local development blockchain
If you are running the Ganache application on your machine. Your node JSON-RPC API endpoint is http://localhost:7545
by default. ganche-cli uses port 8545
1 | Web3j web3 = Web3j.build(new HttpService("http://localhost:7545")); |
Note: As a test network, Ganache doesn’t support all the JSON-RPC API operations specified, for example net_peercount
.
Infura: Hosted nodes for public mainet and testnets
If you use Infura. The node JSON-RPC API endpoint is https://<network>.infura.io/v3/<project key>
.
1 | Web3j web3 = Web3j.build(new HttpService("https://mainnet.infura.io/v3/<project key>")); |
3. Execute API operations
Web3j implements a JSON-RPC API client for Ethereum which can be used in the following way <response> = web3.<operation>.send()
. For example:
1 | try { |
Note: Serilization of the JSON-RPC request can raise an IOException
exception, so you need to handle it.
Result
The following code shows the entire Java program which connects to an Ethereum node and runs some JSON-RPC calls.
1 | //Main.java |
Full file available here
To run the program, right-click on the file Main.java
and click on Run As > Java Application. You should see in the console the following result.
1 | Connecting to Ethereum ... |