官术网_书友最值得收藏!

Using the command line to compile and run Kotlin code

To write and execute code written in Kotlin, you will need its runtime and the compiler. At the time of writing, the stable release of Kotlin is 1.3.31. Every runtime release comes with its own compiler version. To get your hands on it, navigate to https://github.com/JetBrains/kotlin/releases/tag/v1.3.31, scroll to the bottom of the page, and download and unpack the ZIP archive, kotlin-compiler-1.3-31.zip, to a known location on your machine. The output folder will contain a directory called bin with all the scripts required to compile and run Kotlin on Windows, Linux, or macOS. You need to make sure the bin folder location is part of your system path in order to call kotlinc without having to specify the full path.

If your machine runs Linux or macOS, there is an even easier way to install the compiler by using sdkman. All you need to do is execute the following commands in a Terminal:

$ curl -s https://get.sdkman.io | bash
$ bash
$ sdk install kotlin 1.3.31

Alternatively, if you are using macOS and you have homebrew installed, you could run the following commands to achieve the same thing:

$ brew update
$ brew install kotlin@1.3.31

Now that all of this is done, we can finally write our first Kotlin code. The application we will be writing does nothing other than display the text Hello World! on the console. Start by creating a new file named HelloWorld.kt and type the following:

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

From the command line, invoke the compiler to produce the JAR assembly, as follows (include-runtime is a flag for the compiler to produce a self-contained and runnable JAR by including the Kotlin runtime in the resulting assembly):

kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar

Now you are ready to run your program by typing the following on your command line. Make sure that your JAVA_HOME variable is set and added to the system path:

$ java -jar HelloWorld.jar

The code is pretty straightforward. It defines the entry point function for your program, and, in the first and only line of code, it prints the text to the console.

If you have been working with the Java or Scala languages, you might raise an eyebrow because you noticed the lack of the typical class that would normally define the standard static main program entry point. How does it work then? Let's have a look at what actually happens. First, let's just compile the preceding code by running the following command. This will create a HelloWorld.class in the same folder:

$ kotlinc HelloWorld.kt

Now that we have the bytecode generated, let's look at it by using the javap tool available with the JDK, as follows (please note that the file name contains a suffix, Kt):

$ javap -c HelloWorldKt.class

Once the execution completes, you should see the following printed on your Terminal:

    Compiled from "HelloWorld.kt"
    public final class HelloWorldKt {
      public static final void main(java.lang.String[]);
        Code:
          0: aload_0
          1: ldc           #9                  // String args
          3: invokestatic  #15                 // Method  kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull:(Ljava/lang/Ob ject;Ljava/lang/String;)V
          6: ldc           #17                 // String Hello, World!
          8: astore_1
          9: nop
          10: getstatic     #23                 // Field  java/lang/System.out:Ljava/io/PrintStream;
          13: aload_1
          14: invokevirtual #29                 // Method  java/io/PrintStream.println:(Ljava/lang/Object;)V
          17: return
    }

You don't have to be an expert in bytecode to understand what the compiler has actually done for us. As you can see in the snippet, a class has been generated for us, and it contains the program entry point with the instructions to print Hello World! to the console.

I would not expect you to work with the command-line compiler on a daily basis; rather, you should use the tools at hand to delegate this, as we will see shortly.

主站蜘蛛池模板: 清苑县| 金沙县| 阜南县| 宝鸡市| 若羌县| 新津县| 绥化市| 邳州市| 南汇区| 大城县| 峨边| 会同县| 泗阳县| 台湾省| 永丰县| 精河县| 苏尼特右旗| 乌苏市| 辽阳市| 芷江| 磐石市| 黄山市| 富顺县| 新蔡县| 甘孜县| 温泉县| 高雄县| 曲水县| 观塘区| 汽车| 杂多县| 景泰县| 舟山市| 休宁县| 溧水县| 横山县| 武威市| 涟源市| 孟村| 上栗县| 特克斯县|