Hello, World Program is a basic Simple First program or application to code and learn any new programming language.
It just displays the hello world string to the console.
This post shows how to write a Hello World application in Kotlin Programming language.
How do you write Hello World in Kotlin?
The following are steps to run the Kotlin class from the command line.
- Open Text editor
- Create a helloworld.kt file and copy the below code, save the file
fun main(args: Array<String>) {
println("Hello World.")
}
- Open terminal, compile helloworld.kt file using kotlin compiler (kotlinc)
kotlinc helloworld.kt
Generates class files
Run
kotlin helloworld.class
command.It prints the
Hello World.
to the console.
Kotlin Hello World example program
here is an example program to print Hello World to the console.
package com.w3schoolsio;
/*
* Hello.kt
* Kotlin Hello World Example
*/
fun main(args: Array<String>) {
println("Hello World. Welcome")
}
The program contains the following components.
- comments
Comments are useful text about the line of code that is ignored by the compiler. Comments can be single or multi-line comments.
/*
* Hello.kt
* Kotlin Hello World Example
*/
- the main function
It is a function and entry point for the Kotlin application.
func is a keyword in Kotlin.
The
main
function takes an array of strings from the command line.
fun main(args : Array<String>) {
}
- println statement
println() is a function that prints the string inside double quotes to a standard output stream such as a console with an insertion new line.
How to compile and run the Kotlin application
kotlinc
is a kotlin compiler used to compile and execute a program
kotlinc helloworld.kt
It generates helloworld.class in the com/w3schoolsio folder.
To run
kotlin com/w3schoolsio/helloworld.class
Hello World. Welcome
How to Package as a JAR file in Kotlin?
Below are steps to generate a self-executable jar file and execute from the terminal in Kotlin. You have written a program, compiled and executed the program in the command line. In real-time, You make the Kotlin program as a JAR file and run the program.
For example, here is an example to read command line arguments
fun main(args: Array<String>) {
println("command line arguments " + args[0])
}
Following execute the Kotlin compiler command with the -include-runtime option package and generate an executable jar file.
kotlinc helloworld.kt -include-runtime -d helloworld.jar
Next, Run the program from the executable jar by supplying command line arguments.
java -jar helloworld.jar john
It is similar to running a Java executable jar file and Output
command line arguments john
How do I run a JAR file from the command line?
- Create a helloworld.kt file in a Text editor and copy the below code, save the file
fun main(args: Array<String>) {
println("Hello World.")
}
- Open terminal, compile helloworld.kt file using kotlin compiler (kotlinc)
kotlinc helloworld.kt
- Following kotlin compiler command with -include-runtime option package and generates class files and package as an executable jar file.
kotlinc helloworld.kt -include-runtime -d helloworld.jar
Next, execute the jar file the program from the executable jar by supplying command line arguments.
java -jar helloworld.jar john