Install IntelliJ IDEA Community Edition and Write Hello Kotlin Program

To write Kotlin program you will need an IDE (Integrated Development Environment). One of the IDE that you can use is the IntelliJ IDEA CE (Community Edition). It comes with Kotlin Java Runtime Library so you don’t need to install it separately.

To run Kotlin program you will need to install the latest JDK (Java Development Kit) which can be downloaded freely from the Java Download Website. Download version for your operating system and run the installer.

To check if Java successfully installed you can type the following command in your terminal or command prompt:

java -version

If you see something this then Java Development Kit is installed in your system.

java version "10" 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10+46)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)

After installing the JDK you can download the IntelliJ IDEA CE. Double click the installer to install it. For more details on installation and setup you can checkout this website.

Now let’s create our first simple Kotlin program in IntelliJ IDEA.

  • Open the IntelliJ IDEA CE and click the Create New Project from the Welcome Screen dialog.
Create New Kotlin Project

Create New Kotlin Project

  • On the New Project dialog choose Kotlin from the left sidebar and then choose Kotlin/JVM in the selection on the right sidebar. Press the Next button.
New Kotlin Project Setup

New Kotlin Project Setup

  • Enter the project name Hello Kotlin, the project location path and the JDK will be used in the project.
New Kotlin Project Configurations

New Kotlin Project Configurations

  • Click the Finish button to create the project. It will bring you to the following screen.
HelloKotlin Project in IntelliJ IDEA CE

HelloKotlin Project in IntelliJ IDEA CE

  • Right click on the src directory and choose NewKotlin File/Class from the menu. Enter HelloKotlin in the File/Class name.
New Kotlin File/Class Dialog

New Kotlin File/Class Dialog

  • In the HelloKotlin.kt type in the following code snippet.
fun main(args: Array<String>) {
    if (args.isEmpty()) {
        println("Hello, World!")
        return
    } else {
        println("Hi, hello ${args[0]}!")
    }
}
  • To run it, right click on the editor and choose the Run HelloKotlinKt from the menu.
  • If you want to run it with an argument you can set it in the Run/Debug Configurations dialog.
  • To open the Run/Debug Configurations click the down-arrow button next to HelloKotlinKt in the navigation bar on the top-right and choose Edit Configurations…
  • Type in the arguments in the Program arguments textbox.
Run/Debug Configurations

Run/Debug Configurations

That’s all. Now you have your JDK, IntelliJ IDEA CE installed and created your first Kotlin program. If you have any questions please post it in the comments section below. Thank you and have fun with Kotlin.

How do I write Hello World in Kotlin?

In Kotlin, the HelloWorld.kt program can be written as a simple function like the following snippet.

fun main(args: Array<String>) {
    println("Hello, World!")
}

From this little code snippet we can learn the following features of the Kotlin programming language:

  • Kotlin program saved in a file with .kt extension.
  • The fun keyword to declare a function to show you that programming can be fun again 😉
  • To declare variables we start with the variable name followed by its type separated by a colon.
  • We don’t have to end a statement with a semicolons.
  • To print we can use the println function, which is a wrapper to Java System.out.println.
  • The main function is the execution entry point of our HelloWorld program.
  • We can create functions at the top level file without creating a class.

To compile the HelloWorld.kt program we run the following command:

kotlinc HelloWorld.kt

The compiler creates a class file called HelloWorldKt.class. To run it type the following command, assumed that you’ve setup the $KOTLIN_LIB environment variable. In my case the variable is set to /usr/local/Cellar/kotlin/1.2.40/libexec/lib.

java -cp $KOTLIN_LIB/kotlin-stdlib.jar:. HelloWorldKt

How do I create a Hello World Applet?

The code below demonstrate the very basic of Java applet. Applet is a small Java application that can be embedded on the web browser.

package org.kodejava.example.applet;

import java.applet.*;
import java.awt.*;

public class HelloWorldApplet extends Applet {
    public void init() {
    }

    public void start() {
    }

    public void stop() {
    }

    public void destroy() {
    }

    public void paint(Graphics g) {
        g.setColor(Color.GREEN);
        g.drawString("Hello World", 50, 100);
    }
}

To display the applet we need to create an HTML document. Here is a simple example of the document.

<html>
<head>
    <title>Hello World Applet</title>
</head>

<body>
    <applet 
        code="org.kodejava.example.applet.HelloWorldApplet" 
        height="250" 
        width="250">
    </applet>
</body>
</html>

You can now load the applet in your browser or by using the appletviewer utility.

How do I create a HelloWorld Servlet?

Servlet is a Java solution for creating a dynamic web application, it can be compared with the old CGI technology. Using Servlet we can create a web application that can display information from database, receive information from a web form to be stored on the application database.

This example show the very basic of servlet, it returns a hello world html document for the browser. At the very minimum a servlet will have a doPost() and doGet() method which handles the HTTP POST and GET request.

package org.kodejava.example.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloWorld extends HttpServlet {

    public HelloWorld() {
        super();
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse res) 
            throws ServletException, IOException {
        doPost(req, res);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse res) 
            throws ServletException, IOException {
        res.setContentType("text/html");

        PrintWriter writer = res.getWriter();
        writer.println("<html>");
        writer.println("<head><title>Hello World Servlet</title></head>");
        writer.println("<body>Hello World! How are you doing?</body>");
        writer.println("</html>");
        writer.close();
    }
}

For a servlet to works on a servlet container such as Apache Tomcat we need to add or register the servlet in the application’s web.xml file. This configuration tells the container about our servlet class and a url that maps a request to the servlet.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>kodejava-example</display-name>
    <servlet>
        <description></description>
        <display-name>HelloWorld</display-name>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>
            org.kodejava.example.servlet.HelloWorld
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>
</web-app>

When the servlet is deployed to the container we can access it from a url in a form of http://localhost:8080/app-name/HelloWorld.

How do I write Hello World program in Java?

Hello World is a classic program example to start with when we begin to learn a new programming language. The main purpose is to know the basic syntax of the language. Below is the Java version of a Hello World program, it simple enough to start.

package org.kodejava.example.basic;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

The code contains one class called HelloWorld, we declare a class using the class keyword. A main(String[] args) method with a public static void signature and an argument of string array is a special method in Java class. This method is the execution entry point of every Java application. When a class have this method it will be executable. And finally in the body of the method we have a single line of code that write a Hello World string to the console.

The HelloWorld class must be saved in a file named HelloWorld.java, the class file name is case sensitive. In the example we also define a package name for the class. In this case the HelloWorld.java must be placed in a org.kodejava.example.basic directory.

To run the application we need to compile it first. I assume that you have your Java in your path. To compile it type:

javac -cp . org.kodejava.example.basic.HelloWorld.java

The compilation process will result a file called HelloWorld.class, this is the binary version of our program. As you can see that the file ends with .class extension because Java is everything about class. To run it type the command bellow, class name is written without it extension.

java -cp . org.kodejava.example.basic.HelloWorld

And you will see a string Hello World! is printed out in the console as the output of your first Java program, the HelloWorld program.

To install Java Development Kit (JDK) and configure Java environment you can see the following tutorial: