Getting Started with Ballerina


You can download ballerina runtime and tooling from Ballerina runtime contains the runtime environment (bre) needed to run ballerina main programs and ballerina services. Ballerina tooling distribution contains the runtime environement (bre), The Composer (visual editor), Docerina (for API document generation) and Testerina (Test framework for ballerina).

However, for our first ballerina program, only the runtime environment (bre) is sufficient.

First Main Program

To get the things started, lets write a very simple ballerina main program which will print some text to the console. It will look like follow:

import ballerina.lang.system;

function main(string[] args) {
    system:println("My first ballerina main program!");

Now lets save this program as myFirstMainProg.bal.

Run the main Program:

To run our main program, execute the following

<ballerina_home>/bin/ballerina run main <path/to/bal/file>myFirstMainProg.bal

In the console, you would see the below output.

My first ballerina main program!

First Ballerina Service

Now that we have written an executed a main program, it's time to write our first service with ballerina. Lets try to write a simple service that will print the same text as our main program. I will be writing my service to be executed with a http GET request.

import ballerina.lang.system;

service echoService {

    resource echoResource (message m) {
        system:println("My first ballerina service!");
        reply m;

Lets save this service as myFirstService.bal

Run the Service:

To run our service, execute the following:

<ballerina_home>/bin/ballerina run service <path/to/bal/file>myFirstService.bal

In the console, you would see the below output.

ballerina: deploying service(s) in 'passthrough.bal'
ballerina: started server connector http-9090

And unlike in the main program, server will not exit, and will be keep listening to the port 9090. Now to invoke our service, lets run the following curl command, which will send a get request to the service. Note that, in our service, "myService" will be the base path followed by the resource path "echo".

curl http://localhost:9090/myService/echo

Once the request is sent to our service, following will be printed in the console of the service.

My first ballerina service!

Thus, as you see, despite whether it is a main program or a http service, its pretty easy to write and execute them with ballerina!.