Ray Rahke
03/13/2024, 7:21 AMkotlinc Main.kt -include-runtime -d out.jar && java -jar out.jar
, I get unresolved errors about my package imports.hfhbd
03/13/2024, 7:24 AMRay Rahke
03/13/2024, 7:25 AMhfhbd
03/13/2024, 7:26 AMRay Rahke
03/13/2024, 7:27 AMhfhbd
03/13/2024, 7:27 AMRay Rahke
03/13/2024, 7:27 AMRay Rahke
03/13/2024, 7:28 AMhfhbd
03/13/2024, 7:28 AMRay Rahke
03/13/2024, 7:29 AMRay Rahke
03/13/2024, 7:29 AMhfhbd
03/13/2024, 7:31 AMRay Rahke
03/13/2024, 7:32 AMThen you might need to add the jars/files to kotlinc. (edited)could you clarify what you mean by this. I have this structure src/ main.kt foo/ A.kt B.kt
Ray Rahke
03/13/2024, 7:32 AMimport foo.A
fun main() {
A()
}
Ray Rahke
03/13/2024, 7:32 AMpackage
declarations)Ray Rahke
03/13/2024, 7:34 AMhfhbd
03/13/2024, 7:34 AMRay Rahke
03/13/2024, 7:34 AMRay Rahke
03/13/2024, 7:35 AMRay Rahke
03/13/2024, 7:35 AMRay Rahke
03/13/2024, 7:35 AMRay Rahke
03/13/2024, 7:35 AMRay Rahke
03/13/2024, 7:37 AMsudo kotlinc src -include-runtime -d out.jar && java -jar outjar
Error: Unable to access jarfile outjar
Ray Rahke
03/13/2024, 7:38 AMRay Rahke
03/13/2024, 7:38 AMRay Rahke
03/13/2024, 7:38 AMRay Rahke
03/13/2024, 7:39 AM-cp main.jar:package_a
...but that is for a single package.Ray Rahke
03/13/2024, 7:41 AMRay Rahke
03/13/2024, 7:41 AMJoffrey
03/13/2024, 7:41 AMRay Rahke
03/13/2024, 7:41 AMJoffrey
03/13/2024, 7:42 AMbut the play button doesn't do that..What do you mean?
hfhbd
03/13/2024, 7:42 AMRay Rahke
03/13/2024, 7:42 AMhfhbd
03/13/2024, 7:43 AMJoffrey
03/13/2024, 7:43 AMRay Rahke
03/13/2024, 7:43 AMRay Rahke
03/13/2024, 7:43 AMhfhbd
03/13/2024, 7:43 AM./gradlew :compileKotlin
hfhbd
03/13/2024, 7:44 AM./gradlew assemble
Ray Rahke
03/13/2024, 7:47 AMRay Rahke
03/13/2024, 7:47 AMJoffrey
03/13/2024, 7:47 AMi assume it can just recursively resolve imports like all other languagesThat's not how most JVM languages work, btw
hfhbd
03/13/2024, 7:48 AMbuild/libs/name.jar
Ray Rahke
03/13/2024, 7:48 AMRay Rahke
03/13/2024, 7:48 AMRay Rahke
03/13/2024, 7:48 AMRay Rahke
03/13/2024, 7:48 AMhfhbd
03/13/2024, 7:49 AMRay Rahke
03/13/2024, 7:49 AMRay Rahke
03/13/2024, 7:50 AMRay Rahke
03/13/2024, 7:52 AMRay Rahke
03/13/2024, 7:52 AMjava -jar build/libs/kts.jar
no main manifest attribute, in build/libs/kts.jar
Ray Rahke
03/13/2024, 7:52 AMJoffrey
03/13/2024, 7:54 AMall i'm conveying is that when you execute a top-level file like main.whatever, and main.whatever references other files, some mechanism resolves thoseYou don't execute a source file. You compile a set of source files, then you execute compiled files/classes/jars. In the JVM world, you can compile multiple parts independently. So you need to tell the compiler which files you're compiling together, and which dependencies need to be available to them. Your examples are about running the app, which is different. And actually Python and JS are not compiled, so you can't really compare that to a compilation. Rust uses cargo as a build tool, so if you're comparing with this you should compare with Gradle commands, and you get pretty much the same convenience
Ray Rahke
03/13/2024, 7:54 AMhfhbd
03/13/2024, 7:54 AMsets
it for you.Ray Rahke
03/13/2024, 7:55 AMmanifest {
attributes["Main-Class"] = "MainKt"
}
Ray Rahke
03/13/2024, 7:55 AMJoffrey
03/13/2024, 7:55 AMapplication
plugin in Gradle, so it knows it's not a library but an application you can run. You don't need to manually set manifest entries, Gradle will do it for you.
https://kotlinlang.org/docs/get-started-with-jvm-gradle-project.html#explore-the-build-script
plugins {
kotlin("jvm") version "1.9.23"
application
}
application {
mainClass.set("MainKt") // The main class of the application
}
hfhbd
03/13/2024, 7:57 AMapplication.mainClass.set("MainKt")
Joffrey
03/13/2024, 7:58 AMhfhbd
03/13/2024, 7:58 AM./gradlew run
hfhbd
03/13/2024, 7:59 AM./gradlew test
Joffrey
03/13/2024, 8:01 AMbuild.gradle.kts
with:
plugins {
kotlin("jvm") version "1.9.23"
application
}
repositories {
mavenCentral()
}
application {
mainClass.set("MainKt")
}
Then place your source files in src/main/kotlin
, with one of them called Main.kt
with the main function.
Then you can compile and run your app using ./gradlew run
. If you just want compilation or test you can run other Gradle tasks.Ray Rahke
03/13/2024, 8:02 AMsrc/main.kt
, not src/main/kotllin/Main.kt
Ray Rahke
03/13/2024, 8:04 AMplugins {
kotlin("jvm") version "1.9.23"
application
}
repositories {
mavenCentral()
}
application {
mainClass.set("MainKt")
}
dependencies {
testImplementation(kotlin("test"))
}
sourceSets {
main {
kotlin.srcDirs("src/")
}
test {
kotlin.srcDirs("test/")
}
}
Joffrey
03/13/2024, 8:08 AMRay Rahke
03/13/2024, 8:08 AM/gradlew run 3.66s
> Task :run FAILED
Error: Could not find or load main class MainKt
Caused by: java.lang.ClassNotFoundException: MainKt
FAILURE: Build failed with an exception.
Ray Rahke
03/13/2024, 8:09 AMpackage <http://org.me|org.me>
import mod.Foo
fun main() {
Foo()
}
Joffrey
03/13/2024, 8:09 AMpackage <http://org.me|org.me>
at the top, so your main class is not just MainKt
but org.me.MainKt
Ray Rahke
03/13/2024, 8:10 AMRay Rahke
03/13/2024, 8:10 AMJoffrey
03/13/2024, 8:11 AMRay Rahke
03/13/2024, 8:12 AMhfhbd
03/13/2024, 8:14 AMsrcDirs
?Ray Rahke
03/13/2024, 8:14 AMJoffrey
03/13/2024, 8:15 AMJoffrey
03/13/2024, 8:15 AM