‘.kt’ is the extension for kotlin
files. Create a file ‘HelloWorld.kt’ with below content.
HelloWorld.kt
fun main(args:Array<String>){ println("Hello World") }
Keyword ‘fun’
is used to define a function.
‘main’
is the main function of the application. Program execution starts from here.
args:Array<String>
In
Kotlin variable name is declared first, followed by the type. In above case,
args is variable of type array of strings.
println("Hello
World")
Above
statement prints the message ‘Hello World’ to the console. As you see, unlike
java, Kotlin statements do not end with ‘;’.
Compile HelloWorld.kt
Open command prompt and type the command ‘kotlinc HelloWorld.kt’.
Open command prompt and type the command ‘kotlinc HelloWorld.kt’.
C:\Users\Krishna\Documents\Study\Kotlin\Programs>kotlinc HelloWorld.kt C:\Users\Krishna\Documents\Study\Kotlin\Programs>dir Volume in drive C is OSDisk Volume Serial Number is 1034-4F6F Directory of C:\Users\Krishna\Documents\Study\Kotlin\Programs 09/02/2017 11:18 AM <DIR> . 09/02/2017 11:18 AM <DIR> .. 09/02/2017 11:09 AM 57 HelloWorld.kt 09/02/2017 11:18 AM 980 HelloWorldKt.class 09/02/2017 11:18 AM <DIR> META-INF 2 File(s) 1,037 bytes 3 Dir(s) 63,190,188,032 bytes free
As
you see the output of dir command, you can observe, kotlin generates
‘HelloWorldKt.class’ and a META-INF directory.
Run HelloWorld.kt
You can run the HelloWorldkt file using ‘kotlin HelloWorldKt’ command.
You can run the HelloWorldkt file using ‘kotlin HelloWorldKt’ command.
C:\Users\krishna\Documents\Study\Kotlin\Programs>kotlin HelloWorldKt Hello World
Creating and run jar
You
can also create a jar file and run the jar file using java launcher.
Use
the command ‘kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar
‘
to create a jar file.
The
‘include-runtime’ option tells kotlin compiler, include kotlin run time in HelloWorld.jar
file.
C:\Users\Krishna\Documents\Study\Kotlin\Programs>kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar C:\Users\Krishna\Documents\Study\Kotlin\Programs>dir Volume in drive C is OSDisk Volume Serial Number is 1034-4F6F Directory of C:\Users\Krishna\Documents\Study\Kotlin\Programs 09/02/2017 11:24 AM <DIR> . 09/02/2017 11:24 AM <DIR> .. 09/02/2017 11:24 AM 869,262 HelloWorld.jar 09/02/2017 11:09 AM 57 HelloWorld.kt 2 File(s) 869,319 bytes 2 Dir(s) 63,401,816,064 bytes free
Once
the jar file is created, you can call the jar file, just like how you run other
jar files. Use the command ‘java -jar HelloWorld.jar’
C:\Users\Krishna\Documents\Study\Kotlin\Programs>java -jar HelloWorld.jar Hello World
No comments:
Post a Comment