How to setup HyperLedger Fabric smart contract Java environment on Visual Studio Code?
By: Mehmet Emin Duran
Tags: Vscode, Smart Contracts, Java, Visual Studio Code, Hyperledger Fabric

Photo by Luca Bravo on Unsplash

In this tutorial, we will see how to set up an environment to execute Java smart contracts or chaincodes on Visual Studio Code. Since our operating system is Ubuntu, for now, all of the commands executed on the terminal are valid for Ubuntu users. Firstly, you should install HyperLedger Fabric on your machine and have an IBM Blockchain platform extension for VS Code.

In addition to the installations related to HyperLedger Fabric, the gradle is installed with the

sudo apt-get install gradle

command from the terminal.

Then, create Java smart contract project from Visual Studio Code by using IBM Blockchain Platform extension.

After these required installations, we need to do the following steps;

VS Code Extensions:

Install Java extension pack from Visual Studio Code.

Set JAVA_HOME Path

Run command below on the terminal.

readlink -f $ (which java)

The returned result (/usr /lib/vm/java-11-openjdk-amd64) is set to the JAVA_HOME parameter with the following command.

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 echo $JAVA_HOME

Add JAVA bin directory to the PATH variable

export PATH=$PATH:$JAVA_HOME/bin echo $PATH

Config Settings

Search by typing java.home in the Vs Code Settings section. Click “Edit in settings.json” button.

“Java.configuration.runtimes” and “java.home” fields are added to the opened settings.json file as in the image below.

Note: The paths given should show the folder where the java jdk is located.

Running Tests :

The following configurations are added to the build.gradle file. If an error is received, the visual studio code command palette (ctrl + shift + p) is clicked on the Clean Java Language Server Workspace. And, click “Delete and restart”.

ext { jdkVersion = "11" } dependencies { compile "org.junit.platform:junit-platform-commons:1.6.0" testCompile "org.junit.platform:junit-platform-engine:1.6.0" }

Problems and Solutions

The import org.junit.jupiter cannot be resolved vscode error

Add below configuration to the dependincies section on the build.gradle file.

dependencies {

testImplementation(‘org.junit.jupiter:junit-jupiter:5.6.1’)

}

Task ‘shadowJar’ not found in root project error.

Change configuration of build.gradle file as below.

plugins { id ‘com.github.johnrengelman.shadow’ version ‘5.1.0’ } shadowJar { baseName = ‘chaincode’ version = null classifier = null mainClassName=’org.hyperledger.fabric.samples.fabcar.FabCar’ manifest { attributes ‘Main-Class’: ‘org.hyperledger.fabric.samples.fabcar.FabCar’ } }

Note: “mainClassName” and attributes “Main-Class” parameters should be your project’s smart contract class.

After these required steps, you can run your java smart code on Vs Code.

References :

https://hyperledger-fabric.readthedocs.io/en/latest/prereqs.html

https://cloud.ibm.com/docs/blockchain/howto?topic=blockchain-develop-vscode

https://code.visualstudio.com/docs/java/java-tutorial

https://medium.com/@tariqul.islam.rony/learning-java-and-spring-boot-with-visual-studio-code-vscode-part-1-54073f2fa264

https://vitux.com/how-to-setup-java_home-path-in-ubuntu/