java - Gradle 多模块 Kotlin 应用程序给出主类未找到错误
问题描述
我创建了一个小项目,其中包含多个模块和很少的代码。我刚刚设置了它,所以只有几乎没有代码的文件夹。结构是这样的:-
demo(root folder)
-- application(module 1)
-- src
-- main
-- kotlin
--com.demo
-- App.kt
-- build.gradle.kt
-- login (module 2)
-- build.gradle.kt
-- (other build, IDE and gradle folders)
gradle run
当我在演示目录中时,我想通过命令(我猜这是使用 gradle 应用程序插件运行项目的默认方式)从 gradle 运行 App.kt 文件(我认为这可能吗?)。我可以轻松地从 IDE 运行 App.kt,也可以gradle run
在我在应用程序模块中时运行。下面是根目录下 build.gradle 文件的内容:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.2.0.RELEASE" apply false
id("io.spring.dependency-management") version "1.0.8.RELEASE" apply false
kotlin("jvm") version "1.4.30"
kotlin("plugin.spring") version "1.3.50" apply false
kotlin("kapt") version "1.4.10"
application
}
repositories {
jcenter()
}
subprojects {
repositories {
mavenCentral()
jcenter()
}
apply {
plugin("io.spring.dependency-management")
}
}
dependencies {
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
application {
// Define the main class for the application.
mainClassName = "com.demo.AppKt"
}
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
}
}
tasks.test {
useJUnit()
useJUnitPlatform()
}
但是gradle run
从根目录运行会给我这个错误:
Error: Could not find or load main class com.demo.AppKt
Caused by: java.lang.ClassNotFoundException: com.demo.AppKt
到目前为止,我已经看到与我的问题相关的各种其他答案,我已经在 build.gradle 中设置了主类,没有错误。我有什么遗漏或我做错了吗?任何帮助将不胜感激。提前致谢!
编辑:包名有错别字
解决方案
您通往主课的路径错误。在您的目录描述中,我看到了路径com.demo.App.kt
尝试从 更改mainClassName = "com.isdb.AppKt"
为mainClassName = "com.demo.AppKt"
推荐阅读
- mysql - MySQL 在查询中 ROWNUM 的替代方法是什么?
- azure - 从 Jenkins 管道部署 Azure Webjobs
- html - Rselenium 和带有 wbr 的对象
- python - Python (pandas):使用 pandas API 的装饰器
- html - wkhtmltopdf 始终为阿拉伯字符应用默认字体
- java - 将arraylist的第一个元素分配给另一个arraylist
- javascript - 如何在 Ajax 上发送对象和变量
- jekyll - 在 Github 页面中使用工件作为 Jekyll 资产
- xml - xsl:accumulator 如何用于 xsl:function 内部的某些缓存实现以加快评估速度?
- node.js - 如何使用 Node.js 从不同的客户 Stripe 批量收取多笔费用?