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

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.

主站蜘蛛池模板: 高密市| 石棉县| 拜城县| 漳浦县| 阿巴嘎旗| 东明县| 南安市| 民乐县| 余江县| 绵竹市| 中西区| 金平| 武平县| 大兴区| 铁力市| 夏邑县| 田东县| 涞源县| 盖州市| 南召县| 秭归县| 清远市| 民权县| 兴化市| 上杭县| 宝清县| 安龙县| 德钦县| 镇康县| 彭州市| 昌邑市| 海门市| 嘉定区| 彩票| 泸定县| 启东市| 通海县| 谷城县| 高台县| 梨树县| 洱源县|