[英]Gradle can execute TornadoFX app without main() while IntelliJ can't…why?
I've just set up a simple Hello World project with the following code in Kotlin + TornadoFX with IntelliJ IDEA Community.我刚刚在 IntelliJ IDEA 社区的 Kotlin + TornadoFX 中使用以下代码设置了一个简单的 Hello World 项目。 For some reason, if I type gradlew run
at the command line, it manages to run the app by loading the MainApp class directly, but IntelliJ insists "class MainApp has no main() method", refusing to launch the app unless I explicitly write a main()
function.出于某种原因,如果我在命令行键入gradlew run
,它会通过直接加载 MainApp class 来管理应用程序,但 IntelliJ 坚持“类 MainApp 没有 main() 方法”,除非我明确写,否则拒绝启动应用程序一个main()
function。 Why is this?为什么是这样? How can I (or can I at all) get IntelliJ to run the app the same way gradle is managing?我怎样才能(或完全可以)让 IntelliJ 以 gradle 管理的方式运行应用程序?
Here's the Kotlin source file这是 Kotlin 源文件
import tornadofx.*
// Define application with main View `Main`
class MainApp : App(Main::class)
// Define the view to display
class Main : View() {
// override the root view with our container with the label within
override val root = hbox {
// use the tornado kotlin dsl to add a label and set the text
label {
text = "Hello World"
}
}
}
And here's build.gradle.kts这是 build.gradle.kts
plugins {
kotlin("jvm") version "1.3.72"
application
id("org.openjfx.javafxplugin") version "0.0.8"
}
javafx {
// Declare the javafx modules we need to use
modules("javafx.controls")
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("no.tornado:tornadofx:1.7.20")
}
application {
mainClassName = "MainApp"
}
tasks {
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
}
After downloading your test project there are a couple of things I see:下载您的测试项目后,我看到了几件事:
I'm not an expert on Intellij or gradle so I can't explain why you can launch with gradle without main function but can't do so from Intellij.我不是 Intellij 或 gradle 方面的专家,所以我无法解释为什么您可以在没有主 function 的情况下使用 gradle 启动但不能从 Intellij 启动。 I expect that gradle can somehow inject a main function that can execute the JavaFX Application class defined by your MainApp (TornadoFX App class extends JavaFX Application class). I expect that gradle can somehow inject a main function that can execute the JavaFX Application class defined by your MainApp (TornadoFX App class extends JavaFX Application class).
To get this working, you can either setup a run configuration to use gradle or add the main function to MyApp.kt and update the MainApp run configuration.要使其正常工作,您可以设置运行配置以使用 gradle 或将主 function 添加到 MyApp.kt 并更新 MainApp 运行配置。
Use Gradle to Run from Intellij使用 Gradle 从 Intellij 运行
Screenshot of Gradle Panel Gradle面板截图
Note: It is still a good idea to add the main function as described below and update your build.gradle.kts to use "MainAppKt".注意:按如下所述添加主 function 并更新您的 build.gradle.kts 以使用“MainAppKt”仍然是一个好主意。 This will give you some flexibility in launching your application with Java 9+ without requiring module definitions (ie non-modular appplication)这将使您在使用 Java 9+ 启动应用程序时具有一定的灵活性,而无需模块定义(即非模块化应用程序)
Use Intellij to Run使用 Intellij 运行
fun main(args: Array<String>) {
launch<MainApp>(args)
}
Last note : If you are using Java 9+, you need to use a snapshot build of TornadoFX otherwise, you will run into problems.最后注意:如果您使用的是 Java 9+,则需要使用 TornadoFX 的快照构建,否则会遇到问题。
Add the snapshot repository to your respoitories section with将快照存储库添加到您的存储库部分
maven("https://oss.sonatype.org/content/repositories/snapshots")
And update the TornadoFX version to "2.0.0-SNAPSHOT"并将 TornadoFX 版本更新为“2.0.0-SNAPSHOT”
implementation("no.tornado:tornadofx:2.0.0-SNAPSHOT")
Old Answer旧答案
You are missing the mainClassName setting in your build.gradle.kts:您在 build.gradle.kts 中缺少 mainClassName 设置:
application {
mainClassName = "com.example.demo.app.MyApp"
}
Some notes:一些注意事项:
fun main(args: Array<String>) {
launch<MyApp>(args)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.