且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在IntelliJ IDEA中使用Gradle运行main方法?

更新时间:2023-01-11 16:50:50

要设置IntelliJ以运行主类,您只需右键单击 main()方法code> HelloGradle class,然后从菜单中选择Run HelloGradle.main()。您只需执行一次,因为它现在将显示在右上角的运行/配置菜单中,以及您运行的其他任务(即Gradle任务)。输出现在应该显示在控制台中。

To set up IntelliJ to run your main class all you have to do is to right-click the main() method in your HelloGradle class, then choose "Run HelloGradle.main()" from the menu. You only do this once, because it will now show up on the top-right Run/Configuration menu together with other tasks (i.e., Gradle tasks) you run. Output should display in your console now.

对于gradle文件,这就是在Tasks-> build - > ...运行下获取所有Gradle任务所需的全部内容顺利。

For you gradle file, this is all you need to get all the Gradle tasks under Tasks->build->... running smoothly.

group 'com.example'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
   mavenCentral()
}
dependencies {
   testCompile group: 'junit', name: 'junit', version: '4.12'
}
task(runMain, dependsOn: 'classes', type: JavaExec) {
   main = 'com.example.HelloGradle'
   classpath = sourceSets.main.runtimeClasspath
}

以防万一,别忘了点击刷新 按钮,Gradle项目视图中的左上角。

Just in case, don't forget to hit the "refresh" button, the top-left one in the Gradle projects view.

UPDATE1
我添加了任务 Gradle文件中的部分,它运行正常。您可以从Gradle项目 - >运行配置 - > HelloGradle [runMain]运行项目。要查看输出,左下角的运行视图中有一个切换按钮,它的名称为切换任务执行/文本模式,上面带有ab图标;推它,你应该看到输出相同。

UPDATE1: I added the task portion in the Gradle file and it runs fine. You can run the project from the Gradle project->Run Configurations->HelloGradle[runMain]. To see the output, there is a toggle-button in the Run view at the bottom-left, it's called "Toggle tasks execution/text mode" with an "ab" icon on it; push it and you should see the output just the same.

UPDATE2
点击环绕按钮查看输出。

UPDATE2: Click the encircled button to see output.