# Completely stuck starting with Kotlin + JIDEA + Gradle

**URL:** <https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163>\
**Category:** Support\
**Created:** [October 29, 2017, 10:12am UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163 "2017-10-29T10:12:27Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![makistos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/makistos/32/4272_2.png) [@makistos](https://discuss.kotlinlang.org/u/makistos)\
**Post date:** [November 7, 2017, 8:25am UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/21 "2017-11-07T08:25:00Z")

</div>

Check the accepted answer here: [Building a self-executable JAR with Gradle and Kotlin - Stack Overflow](https://stackoverflow.com/questions/26469365/building-a-self-executable-jar-with-gradle-and-kotlin)

This, too, has more or less the same solution: [Kotlin Basics: Create Executable Kotlin JARs, using Gradle | by Preslav Rachev | Medium](https://medium.com/@preslavrachev/kotlin-basics-create-executable-kotlin-jars-using-gradle-d17e9a8384b9)

Funnily enough someone else had the same problem and while you asked him questions you never solved it. The last message links to a solution similar to mine: [Build jar with IDEA throws error but fine with Gradle - #6 by jajacomp](https://discuss.kotlinlang.org/t/build-jar-with-idea-throws-error-but-fine-with-gradle/3794/6)

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [November 7, 2017, 8:33am UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/22 "2017-11-07T08:33:58Z")

</div>

As you can clearly see in your own link, accepted answer is not the most popular. For some reason authors wanted to avoid application plugin and it is just not reasonable. Application plugin not only adds main class to manifest, but also creates a few very useful tasks. If one is keen on using the system not the way it is supposed to be used, no amount of documentation will help.

I recommend to use gradle tasks instead of IDEA build system wherever it is possible. Luckily, IDEA allows to do it very easily (just open gradle sidebar and select a task you want). You can event configure IDEA to always use gradle instead of internal build system, but it does not always work as expected.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [November 7, 2017, 1:34pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/23 "2017-11-07T13:34:04Z")

</div>

@makistos I’ve found this small example of Gradle TornadoFx project: [GitHub - nekonenene/TornadoMemo: Memo Application powered by TornadoFX](https://github.com/nekonenene/TornadoMemo). I hope you find it useful: you can build it with command line or open it in IDEA and compare what is different from your project.

---

<div class="post-metadata">

**Author:** ![makistos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/makistos/32/4272_2.png) [@makistos](https://discuss.kotlinlang.org/u/makistos)\
**Post date:** [November 8, 2017, 7:22pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/24 "2017-11-08T19:22:39Z")

</div>

Hnggggh. Another issue solved. The JRE distributed with IDEA **does not** work with the Gradle plugin. But fixing it from the settings didn’t seem to work either. By recreating the entire project and selecting the JDK version there things seem to work better. At least the project folder and Gradle files now appear and I can add configurations that build and run the project.

Problem is changing this afterwards as IDEA suggests does not seem to help (or user needs to take steps that are not apparent afterwards).

In this setup the application plugin also works and all that other heehaw is not needed.

IDLE still won’t understand Klaxon objects. So even though I can run the application the IDE shows errors for the types and objects.

---

<div class="post-metadata">

**Author:** ![makistos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/makistos/32/4272_2.png) [@makistos](https://discuss.kotlinlang.org/u/makistos)\
**Post date:** [November 8, 2017, 8:38pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/25 "2017-11-08T20:38:40Z")

</div>

The only way I could figure to get that last problem was to clone the Klaxon repository to my machine and then adding it as a module in the Project Structure. Seems silly because one would think the Gradle plugin could automate things.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [November 8, 2017, 8:46pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/26 "2017-11-08T20:46:59Z")

</div>

Maybe you can share the entire project folder so we can check if something is misconfigured?

---

<div class="post-metadata">

**Author:** ![makistos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/makistos/32/4272_2.png) [@makistos](https://discuss.kotlinlang.org/u/makistos)\
**Post date:** [November 8, 2017, 9:22pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/27 "2017-11-08T21:22:04Z")

</div>

Here it is:

> **[GitHub - Makistos/Megafooni](https://github.com/Makistos/Megafooni)**
>
> Contribute to Makistos/Megafooni development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [November 9, 2017, 10:36am UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/28 "2017-11-09T10:36:53Z")

</div>

I can see two immediate problems:

1. You have quite a few of build blocks duplicated your `build.gradle` should look like this:

```groovy
buildscript {
    ext.kotlin_version = '1.1.51'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin'
apply plugin: 'application'

mainClassName = "com.sofistes.megafooni.AppKt"
repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    compile 'com.beust:klaxon:0.30'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

```

1. Your `App.kt` is in wrong directory. Java by default expects a class file with package `com.sofistes.megafooni` to be in `src/main/kotlin/com/sofistes/megafooni` directory. Kotlin should work event with incorrect directory structure, but it is safe to use standard one.

---

<div class="post-metadata">

**Author:** ![makistos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/makistos/32/4272_2.png) [@makistos](https://discuss.kotlinlang.org/u/makistos)\
**Post date:** [November 9, 2017, 11:48am UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/29 "2017-11-09T11:48:51Z")

</div>

That class name is obviously wrong, I’ll fix it later. It just had to be something and that’s what I was familiar with. I did google how to name Java classes after that but is the class name really only AppKt if I put into the kotlin directory (a logical place for the main function, I think)?

That build.gradle looks odd, I don’t even know how those blocks were duplicated.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [November 9, 2017, 12:23pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/30 "2017-11-09T12:23:16Z")

</div>

Class name is OK. Kotlin automatically add Kt to java class output. So it is correct to use `App` in Kotlin filename, but AppKt in java.

Build blocks were dublicated by IDEA’s efforts to add kotlin configuration for your project. As I already said, this feature is broken, you should not use it. Just stick to manually editting `build.gradle` and then refreshing IDEA build (don’t try to manually edit IDEA build configuration).

---

<div class="post-metadata">

**Author:** ![makistos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/makistos/32/4272_2.png) [@makistos](https://discuss.kotlinlang.org/u/makistos)\
**Post date:** [November 9, 2017, 7:24pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/31 "2017-11-09T19:24:58Z")

</div>

That gradle file is not quite enough, using that results in

```
no main manifest attribute, in ./build/libs/Megafooni-1.0-SNAPSHOT.jar

```

if I try to run the jar directly. Adding that jar {} block fixes that.

---

<div class="post-metadata">

**Author:** ![makistos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/makistos/32/4272_2.png) [@makistos](https://discuss.kotlinlang.org/u/makistos)\
**Post date:** [November 9, 2017, 7:30pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/32 "2017-11-09T19:30:46Z")

</div>

That bug is stranger, mind. I didn’t add or edit the configurations, yet I have some stuff duplicated again.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [November 10, 2017, 6:43am UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/33 "2017-11-10T06:43:14Z")

</div>

You are using wrong task. You should use `installDist` instead of `jar`. Then you won’t need any `jar` blocks, just use start script to run application. Otherwise, your jar will be missing dependencies, when you add them. If you want an executable uber-jar with all dependencies, then use [this plugin](https://github.com/johnrengelman/shadow) and task `shadowJar` instead.

As for duplication, it appears then kotlin plugin tries to “correct” your build. Don’t click on any links in warning window.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [November 12, 2017, 5:10pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/34 "2017-11-12T17:10:02Z")

</div>

I’ve checked it out and opened it in IDEA.  
Then I imported Gradle project as IDEA suggested and it was fine. I’ve not spotted anything suspicious, except `../klaxon` directory mapping in `.idea/vcs.xml`.  
Klaxon library and its sources were successfully downloaded from maven central and the references to klaxon API were resolved correctly in IDE.

I’m using IDEA 2017.2.5 with Kotlin plugin 1.1.51-release-IJ2017.2-1, my `JAVA_HOME` points to jdk1.8.0\_144 installation.

What I’ve actually asked is to share the entire project directory with all files, including those that are ignored by git.

---

<div class="post-metadata">

**Author:** ![makistos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/makistos/32/4272_2.png) [@makistos](https://discuss.kotlinlang.org/u/makistos)\
**Post date:** [November 12, 2017, 6:27pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/35 "2017-11-12T18:27:33Z")

</div>

Could you tell what to remove from .gitignore and I’ll do that. I’ll make a specific branch in GitHub with all the missing files. I don’t think you need the build directory for instance and they would take tons of space.

Note that thanks to the help I have received so far I have managed to tackle most of the problems and have actually been able to code a few lines of code & managed to build and debug. The main issue was that Gradle by default used the JRE version that was set as default for the IDE and I couldn’t figure out how to fix this after creating the project. It looked like changing it afterwards just gave me tons of errors. Re-creating the project from scratch fixed the major problems.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [November 13, 2017, 5:26pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/36 "2017-11-13T17:26:04Z")

</div>

> [@makistos](#):
>
> Could you tell what to remove from .gitignore and I’ll do that.

Yeah, everything without build directory would be fine.

> [@makistos](#):
>
> The main issue was that Gradle by default used the JRE version that was set as default for the IDE and I couldn’t figure out how to fix this after creating the project.

You can open Gradle options dialog from Gradle projects pane and switch JVM used to start Gradle there:

 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/c/c5be9805f9defb7d9e3785d71d3d0c6596fcced5.png)

---

<div class="post-metadata">

**Author:** ![makistos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/makistos/32/4272_2.png) [@makistos](https://discuss.kotlinlang.org/u/makistos)\
**Post date:** [November 13, 2017, 5:38pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/37 "2017-11-13T17:38:54Z")

</div>

Ok, that repository now has a branch called “ilya” which should have what you need.

I think I did try to change the JVM from that page and it didn’t help. It’s possible I had something else wrong, too, though.

---

<div class="post-metadata">

**Author:** ![makistos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/makistos/32/4272_2.png) [@makistos](https://discuss.kotlinlang.org/u/makistos)\
**Post date:** [November 16, 2017, 6:54pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/38 "2017-11-16T18:54:23Z")

</div>

Ok, could someone give me step-by-step instructions on how to make IDEA understand a library? I managed to do that with Klaxon and was like “I got this” but now I’m failing with this with TornadoFX. I have added it to build.gradle and compiled the package from inside IDEA but IDEA still complains how the imports fail. And do I need to add javafx as well as IDEA doesn’t like if I import anything from it either (Tornadofx’s instructions don’t mention this)?

I have tried to add the module in couple different places in Project Structure but it doesn’t seem to work.

---

<div class="post-metadata">

**Author:** ![napperley](https://avatars.discourse-cdn.com/v4/letter/n/90db22/32.png) [@napperley](https://discuss.kotlinlang.org/u/napperley)\
**Post date:** [November 20, 2017, 12:46am UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/39 "2017-11-20T00:46:33Z")

</div>

@makistos - What is your software environment? Need to know that following details:

- OS and version (if Linux then the distribution as well)
- JDK distribution and version
- Kotlin version
- IntelliJ edition and version
- Gradle version and which DSL is used (traditional or Gradle Kotlin DSL)

Also it would be very helpful to know a little bit about your software development background. You mention that you have some Python experience, have used VIM, and presumably are comfortable with using a terminal. Which programmer’s text editor do you prefer to use?

A while back I wrote a [Kotlin tutorial](https://medium.com/@napperley/kotlin-tutorial-1-introduction-675816f2443c) that covers setting up software development tools for Kotlin development. Even though the tutorial is out of date and it doesn’t cover Gradle you will learn a lot of valuable information on setting up a proper software development environment, which is extremely important when you want to get started quickly and easily.

---

<div class="post-metadata">

**Author:** ![makistos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/makistos/32/4272_2.png) [@makistos](https://discuss.kotlinlang.org/u/makistos)\
**Post date:** [November 20, 2017, 4:14pm UTC](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163/40 "2017-11-20T16:14:41Z")

</div>

I’m doing this on a Ubuntu 16.10 running in a VirtualBox. For JDK I’m using 1.8, Gradle is 3.3 and Kotlin is 1.1.0 (these taken from gradle.build). IDEA Help → About gives me this:

IntelliJ IDEA 2017.1.3  
Build #IC-171.4424.56, built on May 12, 2017  
JRE: 1.8.0\_131-8u131-b11-0ubuntu1.16.10.2-b11 amd64  
JVM: OpenJDK 64-Bit Server VM by Oracle Corporation  
Linux 4.8.0-59-generic

I use GVim as my programmer’s text editor, been doing that for several years. Also have extensive experience with CCS (Code Composer Studio which is basically built on top of Eclipse). For Python coding I usually use PyCharm, for the rest GVim (C, Perl, Bash shell scripts) or for some functional languages I have dabbled with (Clojure, Haskell, Erlang) a bit of Emacs. So, basically I get paid to develop embedded stuff for telecomms devices. Base stations for a long period, last two years low-level Android (lately bootloader and kernel related things). Now, I don’t like Java so I have tried to keep clear of the upper layers so you can see why I would like to learn a bit of Kotlin.

Yes, I have the Vim plugin in IDEA and PyCharm, too bad it’s quite limited (best case scenario would be that it supported .vimrc files directly).

Main problem trying to set things up is the same you mention, everything is out of date. I have spent hours trying to follow instructions only to figure out in the end that they must be outdated already. Another is there seems to be no logic to all this. I downloaded TornadoMemo above and it doesn’t complain about javafx. My project does and I can’t find any meaningful differences between the two projects. More specifically, IDEA complains, I can compile the code just fine.

[Previous page](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163.md?page=1)

[Next page](https://discuss.kotlinlang.org/t/completely-stuck-starting-with-kotlin-jidea-gradle/5163.md?page=3)
