How to Create and Run First Kotlin Project in Eclipse IDE

According to the Stack Overflow Developer Survey Results, 2019 Kotlin is the 4th most loved programming language and among the top programming languages with the highest salaries worldwide due to its efficiency, 100% interoperability with java, reduced crashes and low cost of adoption.

In this tutorial, we’ll be going through the steps to create and run your first Kotlin project in Eclipse IDE.

Installing Eclipse IDE

  1. For the Kotlin development, you will need "Eclipse IDE for Java developers". If you already have Eclipse IDE for Java developers, you can skip this step. It has everything you need for Kotlin including important extra features like a Git client, XML Editor and Gradle integration for android development. 

  2. The easiest way to get Eclipse IDE for Java developers is through the Eclipse installer. Eclipse installer is the most efficient way to install Eclipse. It is a proper installer (no zip files) with easy to follow steps. You can download the Eclipse installer from here. Keep in mind that you’ll need a Java runtime environment (JRE) to run Eclipse IDE for Java developers which can be downloaded from here.
    Download eclipse ide for java developers

  3. Run the eclipse installer, and choose “Eclipse IDE for Java developers” from the menu. 

  4. Menu Eclipse IDE for Java developers

  5. Choose the path of Java Runtime Environment (JRE) which we downloaded in the first step. Usually, the Eclipse installer will automatically find the location of JRE. 

  6. Choose the desired path to install the Eclipse IDE and click the “Install” button as shown in the picture below. The java installer will automatically download the necessary packages and install them in the right place. 
    How to install Eclipse IDE for Java Developers

  7. After the installation process is completed, click the “Launch” button to start the Eclipse IDE.

Installing Kotlin plugin

  1. Launch the Eclipse IDE for java developers if you haven't already, and choose the desired workspace. The workspace is the folder where all of your project files are present.
    workspace example in Eclipse IDE

  2. Once Eclipse IDE welcome screen appears, click “Launch the Eclipse marketplace” to download Kotlin plugin for IDE. If “Launch the eclipse marketplace” is not there or welcome screen doesn't appear, you can go to help → eclipse marketplace from the menu bar at the top of the IDE. How to launch Eclipse Marketplace

  3. In the eclipse market, type “Kotlin” in the search bar and install “Kotlin plugin for Eclipse”.Kotin plugin in eclipse marketplace

  4. After some processing, a license dialog will appear. After reviewing the terms:

    Select “I accept the terms of the license agreement” and click “Finish”. The eclipse will download and install the Kotlin plugin in the background. The progress bar will be shown in the bottom-right corner of the Eclipse IDE. In this process, IDE may ask to accept some certificates, so accept them after reviewing the terms. Once the downloading and installation process completes, the IDE will open the confirmation dialog. Click the “restart” button to restart the IDE and finish the installation process.Eclipse processing license dialog

 Creating an Eclipse project

  1. To create a new Kotlin project, from the menu bar, click File → Project. The “New Project” dialog will appear. 

  2. From the “New Project” dialog, double click on the “Kotlin” folder and choose the “Kotlin Project” and click the “Next” button. 
    Kotlin project

  3. In the “Kotlin Project” dialog, give your new project a name and choose a location. By default, “Use default location” checkbox is ticked which means that the project will be saved in the workspace folder you choose on the startup of the Eclipse IDE. Finally, click the “Finish” button.
    Project Name in Kotlin

  4. The Eclipse will build the project and save it in the desired folder. But, mostly Kotlin's perspective ( a term of eclipse IDE which means the view and arrangement of different editors and helper) is not open and the eclipse will remain on the welcome screen. To open the Kotlin perspective, click the workbench button on the top-right corner of the screen as shown in the below picture.
    workbench example of kotlin

Running the First program

  1. Once the Kotlin perspective is opened, you need to create a new Kotlin file. All code files for the Kotlin project go in the “src” folder. To create a new file, right-click on the “src” folder, and click New → Other.
    Run the hello world program in kotlin


  2. When the “New” Dialog appears, choose Kotlin → Kotlin file from the menu and click the “Next” button. Write the name of the file in the “name” field. You can leave the package field empty for now. But, it is recommended to organize your files in packages. Click the “Finish” button to create the file.

  3. Double-click on the new file from the “src” folder if it’s not already opened. 

  4. To define the entry point for program execution, we need to define the “main” function here as in most high-level languages. 

  5. Functions in Kotlin start with “fun” keyword following the function name and any arguments in the parentheses. There is no need to provide arguments in main function as they are optional. 

  6. Keeping the tradition of programming, we will write a simple program that will print “Hello World” on the console. The code of the program is as follows:

    fun main()
    {
        println("Hello World")
    } 
    

    Did you notice that there is no semicolon at the end of the statement as there is no need for the semicolon in Kotlin? Now, we do not have to face an endless stream of errors just because of a missing semicolon.

  7. To execute the program, press Ctrl + F11 OR Run -> Run OR if you want to run only a single file, right-click on the file from the “src” folder and click on Run as -> Kotlin Application. The output will be displayed in the console which is located usually in the bottom.
There you have it, our very own first program running in Kotlin. To keep updated with the new tutorials and guidelines regarding Kotlin, Java and other technologies, stay tuned to our blog.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry