hi, I'm stuck with a weird problem. as you can se...
# getting-started
x
hi, I'm stuck with a weird problem. as you can see, I used IDEA to create a new project with gradle to learn kotlin if I add the line
Copy code
package com.xun.kotlin
to the file, notice the error beside the build button. what's wrong with that ? if I comment the line, the error disappear. I used to be a frontend developer, In javascript, there is no need to add the line like this.
e
The reference to
MainKt
was to the package-less name. Now that you've added a package name, that needs to change.
You can either edit the run configuration or remove it and use the "run" action again, which will recreate the run configuration
x
Hi, why you say
MainKt
, but not
Main.kt
? anyway I removed that line, Yes, now I could build and run the code via IDEA but how about use command line ?
Copy code
java -jar path/to/build/libs/result.jar
just show me a error: then I searched a similar question and get a answer like this so I add a line to my
build.gradle.kts
,:
Copy code
tasks.withType<Jar> { manifest { attributes["Main-Class"] = "com.xun.kotlin.MainKt" } }
but I got a another error:
Copy code
xun@xun-Yoga-14sIHU-2021:~/Desktop/kotlin-basic/build/libs$ java -jar kotlin-basic-1.0-SNAPSHOT.jar 
Error: Could not find or load main class com.xun.kotlin.MainKt
Caused by: java.lang.ClassNotFoundException: com.xun.kotlin.MainKt
anyway, I just wanna run the project with the command line, what is wrong?
I mean, we can use IDEA to run the project, but I'd prefer to use command line
e
Kotlin wraps top-level elements of
file.kt
in a
FileKt
class for Java compatibility by default (you can choose a different name with
@file:JVM name("...")
)
to run from the command line, it would be better to use the Gradle application plugin. running as an individual jar like that will result in missing dependencies
Copy code
plugins {
    application
}

application {
    mainClass.set("com.xun.kotlin.MainKt")
}
this will allow you to use
./gradlew run
x
I thought the file
kotlin-basic-1.0-SNAPSHOT.jar
is in build directory so it has all it's dependencies bundled
e
or
./gradlew installDist
which will create a directory
build/install
containing everything needed, or
./gradlew distZip
or
./gradlew distTar
which will create that bundle in
build/distributions
no, dependencies are not bundled in the jar
x
thank you , I've never learn java before so I just code like javascript way😄. thanks again
but I really wanna know why the solution I mentioned didn't get work in my project😀
s
if you want all dependencies to be included into the jar - a so called "fat jar" - you can use the shadow-plugin: https://plugins.gradle.org/plugin/com.github.johnrengelman.shadow
x
Hi, actually I'm follow the graalvm doc(https://www.graalvm.org/22.3/guides/#run-java-applications-on-graalvm-from-the-cli) , it says "If your Java project is built with Gradle, run
./gradlew build
to build the project and then run it from a JAR file as usual (
java -jar <JAR file>
)." I thought the builded jar file is in
build/libs/
, so I went there but still got error: