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

IntelliJ and Kotlin

Coding using Vim/nano is not everyone's first choice. Working without the help of an IDE with its code completion, IntelliSense, shortcuts for adding files, or refactoring code can prove challenging depending on how complex the project is.

For a while now, in the JVM world, people's first choice when it comes to their integrated development environment has been IntelliJ. The tool is made by the same company that created Kotlin—JetBrains. Given the integration between the two of them, it would be my first choice of IDE to use, but, as we will see in the next section, it is not the only option.

IntelliJ comes in two versions—Ultimate and Community (free). For the code we will be using over the course of this book, the free version is sufficient. If you don't already have it installed, you can download it from https://www.jetbrains.com/idea/download.

From version 15.0 onward, IntelliJ comes bundled with Kotlin, but if you have an older version, you can still get support for the language by installing the plugin. Just go to Settings Plugins Install IntelliJ plugins, and type Kotlin in the search box.

We are going to use the IDE to create a Gradle project with Kotlin enabled, just as we did in the previous section. Once you have started IntelliJ, click Create new project. You will then see a dialog window from which you should select Gradle from the left-hand side section;. Then, check the Kotlin(Java) option from the right-hand side, as shown in the following screenshot:

Selecting a project type

You should already have the system variable JAVA_HOME set up for the tool to pick it up automatically (see the Project SDK at the top of the screenshot). If this isn't the case, click the New button and navigate to where your JDK is. Once you have selected it, you are ready to go to the next step by clicking on the Next button available on the bottom right-hand side of the screen.

The next window presented to you asks you to provide the Group Id and Artifact Id. Let's go with com.programming.kotlin and chapter01, respectively. Once you have entered these fields, you can move to the next step of the process where you tick the Use auto-import flag and Create directories for empty directory roots automatically options. Carry on to the next step, where you will be asked where you wish to store the project on your machine. Set the project location, expand More Settings, type chapter01 for the Module name, and hit the Finish button.

IntelliJ will go on and create the project, and you should see the outcome shown in the following screenshot:

Hello World! basic project

With the kotlin folder selected, right-click, select the New | Package option, and type com.programming.kotlin.chapter01, as shown in the following screenshot:

Setting up the package name

You should see a new folder appear below the kotlin folder, matching what was typed earlier. Right-click on that, choose New | Kotlin File/Class, and type Program.kt, as shown in the following screenshot:

Creating the Program.kt file

We are now ready to start typing our Hello World! string. Use the same code we created earlier in the chapter. You should notice the Kotlin brand icon on the left-hand side of the file editor. If you click on it, you will get the option to run the code, and if you look at the bottom of your IntelliJ window, you should see the text Hello World!, printed out, as shown in the following screenshot:

Hello World! program

Well done! You have written your first Kotlin program. It was easy and quick to set up the project and code, and then to run the program. If you prefer, you can have a Maven rather than a Gradle project. When you choose New | Project, you have to select Maven from the left-hand side and check Create from archetype while selecting org.jetbrains.kotlin:kotlin-archetype-jvm from the list presented, as shown in the following screenshot:

Maven project

As the screenshot shows, the Maven option should be selected from the various archetypes.

主站蜘蛛池模板: 大渡口区| 永宁县| 乌兰县| 萍乡市| 青岛市| 玉田县| 奉节县| 邢台县| 得荣县| 丰原市| 南投县| 兴和县| 当阳市| 施甸县| 固安县| 个旧市| 伽师县| 柳林县| 大同市| 潮安县| 扎鲁特旗| 泰兴市| 周宁县| 海林市| 东城区| 禄劝| 桃园市| 灵宝市| 仙游县| 施秉县| 浏阳市| 彰武县| 阿克| 竹山县| 东乌| 襄城县| 盐源县| 涿州市| 商城县| 花莲市| 阿巴嘎旗|