Completely stuck starting with Kotlin + JIDEA + Gradle


#21

Check the accepted answer here: https://stackoverflow.com/questions/26469365/building-a-self-executable-jar-with-gradle-and-kotlin

This, too, has more or less the same solution: 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


#22

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.


#23

@makistos I’ve found this small example of Gradle TornadoFx project: 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.


#24

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.


#25

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.


#26

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


#27

Here it is:


#28

I can see two immediate problems:

  1. You have quite a few of build blocks duplicated your build.gradle should look like this:
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.

#29

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.


#30

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).


#31

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.


#32

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


#33

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 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.


#34

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.


#35

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.


#36

Yeah, everything without build directory would be fine.

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


#37

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.


#38

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.


#39

@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 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.


#40

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.