In our Web3J Ethereum development course, the preset code is built using Gradle at the terminal command line. If you want to develop web3J Ethereum applications in the familiar Eclipse, this quick tutorial will show you how to configure them.
We will break this into six steps to show you how to develop Web3J Ethereum applications using Gradle projects in Eclipse:
- Install the Gradle tool
- Install Eclipse Gradle plugin
- Create a Gradle project
- Configure the Gradle project
- Create demo class code
- Run the emulator and demo code
1. Install Gradle
Download Gradle 4.8.1 and unzip it in a directory such as D :\ Tool. You do not need to configure environment variables, just decompress them.
Note: The Gradle compression package contains one level
Gradle - 4.8.1
Directory, so the final installation directory is:D: \ tool \ gradle - 4.8.1
.
2. Install Gradle plugin
The latest Oxygen version of Eclipse already integrates Gradle by default, so you can create Gradle projects directly:
To manually install, click on the menu __[Help]__-> [Eclipse Marketplace], then search for Gradle and select __Buildship Gradle Integration 2.0__ to install:
Create a Gradle project
Select menu __[New]__ -> [Project…] , select __Gradle Project__ from the wizard dialog box that pops up:
Then give the project a name, like test:
On the project options page, first enable the Override workspace Settings option to fill in the Gradle installation directory, such as d:\tool\gradle-4.8.1:
Configure the Gradle project
Add the web3j and logback dependencies to your dependencies and add the maveCentral() repository to your dependencies:
plugins {
id 'java-library'
}
dependencies {
api 'org.apache.com mons: Commons - math3:3.6.1 track'
implementation : 'com. Google. Guava guava: 23.0'
testImplementation 'junit: junit: 4.12'// Add web3j and logback dependencies compile'org. Web3j: core: 3.3.0'.'ch. Qos. Logback: logback - core: 1.2.3'.'ch. Qos. Logback: logback - classic: 1.2.3'} repositories {jCenter () // Add mavenCentral()}Copy the code
Note: After updating the build.gradle file, be sure to perform a manual refresh for the new Settings to take effect!
Right-click on the Project name in the __Package Explorer__ window and click on __[Gradle]__ -> [Refresh Gradle Project] :
Create a new class
Create a new class App and use the default Settings:
Then modify the app.java code as follows:
package test;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
public class App {
public static void main(String[] args) throws Exception {
Web3j web3j = Web3j.build(new HttpService("http://localhost:8545")); String v = web3j.web3ClientVersion().send().getWeb3ClientVersion(); System.out.println(v); }}Copy the code
6, run,
Start ganache-CLI on your local machine first. For Windows, we recommend using our Ethbox ethereum development kit:
Then click on __[Run]__ -> [Run As] -> [Java Application] :
If everything goes well, you should first see Gradle downloading dependencies on the Eclipse console, and then see the results of your code execution:
BINGO!