Hello World program in Kotlin

To compile and run a Kotlin program, you’ll need to follow these general steps.

1. Write Your Kotlin Code:

    Open notepad or VSCode and Save the code in a file with a .kt extension, for example, HelloWorld.kt

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

    Explanation:

    fun main() { … }:In Kotlin, every program needs a main function, which is the starting point of execution.

    println(“Hello, World!”): It’s built-in function in Kotlin used for printing to the console. it prints the string “Hello, World!” followed by a newline.

    To Run the Program: Make sure you have the Kotlin compiler (kotlinc) installed on your machine.

    2. Compile Your Kotlin Code:

    To compile the program in the command-line compiler, navigate to the directory where your file is saved. Use the kotlinc command to compile your Kotlin code. In the terminal, run:

     E:\programs>kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar
    

    HelloWorld.kt: Replace this with the name of your Kotlin file.
    -include-runtime: It includes the Kotlin runtime in the JAR file.
    -d HelloWorld.jar: It specifies the name of the output JAR file.

    3. Run the Compiled Program:

    After compiling, you’ll get a JAR file (in this case, HelloWorld.jar). Use the java command to run it:

    E:\programs>java -jar HelloWorld.jar
    

    This will execute your Kotlin program, and you will see the output:

     Hello World

    Make sure both compiler (kotlinc) and Java should be installed on your machine. Ensure that the path to these tools should be set correctly in your System Environment variables.

    you can also use integrated development environment (IDE) like IntelliJ IDEA with the Kotlin plugin. It will simplifies the compilation and execution process.

    Leave a Reply