This post shows how to install Ballerina and OpenJDK 8 using ZIP distributions, add on its IntelliJ plugin, and create a Ballerina project in Intellij IDEA. We are going to use OpenJDK 8 because Ballerina 1.2.6 only works on that platform.
Ballerina 1.2.6 Works with JRE/JDK 8
If you are using Java Development Kit beyond version 8, running Ballerina will not work. Consider the following screenshot.
Since Ballerina 1.2.6 only works with JDK 8, we can download an OpenJDK 8. In this post, we are using ZIP distributions and we need to set some environment variables within a command-line window.
Extract OpenJDK and Ballerina ZIP Distributions
To avoid making permanent changes to the operating system, we are going to use ZIP distributions of OpenJDK 8 and Ballerina. For this post, we extracted the files to some directories.
Ballerina IntelliJ Plugin
Next, we install the Ballerina IntelliJ IDEA plugin. Head to the IDE and go to File > Settings... > Plugins. Then, choose Marketplace and search for the plugin using the keyword Ballerina.
Click the Install button and restart IntelliJ.
Create a Ballerina Project in IntelliJ IDEA
When we create a Ballerina project in IntelliJ IDEA for the first time, we need to update the configuration of the plugin. Go to File > New > Project... and choose Ballerina.
Click Next and set the Project SDK to the Ballerina SDK.
Now, the Ballerina SDK resides within the Ballerina zip distribution under the distributions folder. When we extract the zip file, we get the following files and directories.
On the Project SDK, point to the Ballerina SDK within the distributions folder. This is the only part that requires configuration for the Ballerina plugin. Click OK to proceed.
Then, provide the name for the project and its location. Then, click Finish.
We can also add the Ballerina SDK via File > Project Structure... > SDKs.
Lastly, we need to add OpenJDK 8 so that we can run Ballerina codes in IntelliJ.
Hello World Ballerina Sample Codes
Now we created a project, we can start creating codes. If we create a Ballerina file, the plugin will prompt asking us to create the source file within a Ballerina Module.
For this post, let’s create a Ballerina Main something akin to Java class that has the public static void main(String args) function.
The Ballerina plugin will create sample codes as shown below. However, we will encounter an error.
"C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.1\bin\runnerw64.exe" C:/Users/karldev/Desktop/dev/apps/ballerina-1.2.6/distributions/jballerina-1.2.6\bin\ballerina.bat run --sourceroot C:\Users\karldev\Desktop\dev\blogs\ballerina\new-hello-world mymodule
"You must set the JAVA_HOME variable before running Ballerina."
Process finished with exit code 0
To fix this, create a .bat file and place it somewhere appropriate. The file contains the following codes.
Then, modify the ballerina.bat that resides in the Ballerina SDK folder.
Add a call to setenv.bat file as follows.
rem ----- if JAVA_HOME is not set we're not happy ------------------------------
Go back to IntelliJ IDEA and run the Ballerina Main file.
At this point, we successfully installed the Ballerina plugin in IntelliJ IDEA with tweaks in configurations.
Alternative Ballerina Configuration Of JDK
Alternatively, we can extract the JDK 8 zip distribution to the ballerina-1.2.8\dependencies directory. There is one caveat, though. We need to rename the subdirectory to exactly jdk8u202-b08-jre.
We may need to restart IntelliJ IDEA for the new configuration to take effect. With this alternative, we no longer even need to specify the JDK in IntelliJ IDEA.
This is part of the Ballerina Programming Language Tutorial.