yazan sayed
yazan sayed

Reputation: 1139

trying to create a jar executable in IntelliJ IDEA

I'm trying to build a jar file for a tornadofx project,

it runs normally when i run the main function in my.kt (seri/src/main/kotlin/my.kt)

package com.serious

import tornadofx.*
import tornadofx.App
import tornadofx.launch

fun main() {
    launch<MyApp>()
}

class MyApp : App(primaryView = ViewX::class)
//-------------------------
class ViewX : View("My View") {

    override val root = vbox {

        button("add an item") {

        }
    }
}

the build.gradle file

buildscript {
    ext.kotlin_version = "1.2.60"
    ext.tornadofx_version = "1.7.17"
    ext.junit_version = "5.1.0"

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

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.3.31'
    id 'org.openjfx.javafxplugin' version '0.0.7'

}

version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'no.tornado:tornadofx:1.7.18'

}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

javafx {
    version = "12.0.1"
    modules = ['javafx.controls']
}

running windows 10, intelli IDEA 2019.1.1 i made a jar artifact in the project structure,and 'build artifacts' from build menu,, it built a jar file but it's not running, shows error " Error: Could not find or load main class com.serious.MyApp Caused by: java.lang.NoClassDefFoundError: tornadofx/App "

Upvotes: 1

Views: 1884

Answers (2)

Saurabh Potdar
Saurabh Potdar

Reputation: 1

Intellij Gradle

Simplest way is to click on Gradle -> build -> bootJar

Fat jar will be created in build/libs folder

Upvotes: 0

aiscy
aiscy

Reputation: 143

Not sure if it's the optimal solution, but you can use shadow plugin for gradle. It allows to create fat/uber JARs easily. All you need to do is to slightly modify your build.gradle file:

plugins {
    id "application"
    id "org.jetbrains.kotlin.jvm" version "1.3.11"
    id "com.github.johnrengelman.shadow" version "4.0.2" // 5.0.0 for gradle 5.0+
}

mainClassName = "com.serious.MyKt"

repositories {
    mavenLocal()
    mavenCentral()
    maven {
        url "https://oss.sonatype.org/content/repositories/snapshots/"
    }
}

ext {
    tornadofx_version = "1.7.18"
    junit_version = "5.4.0"
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib"
    implementation "no.tornado:tornadofx:$tornadofx_version"
    testImplementation "org.junit.jupiter:junit-jupiter:$junit_version"
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

test {
    useJUnitPlatform()
}

And then run a task named 'shadowJar'. Fat jar will be placed into /build/libs/.

Upvotes: 2

Related Questions