简体   繁体   English

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

[英]How to run main method using Gradle from within IntelliJ IDEA?

Am new to IntelliJ IDEA (using 2017.1.3) and gradle... 是IntelliJ IDEA的新手(使用2017.1.3)和gradle ...

Java file: Java文件:

package com.example;

public class HelloGradle {

    public static void main(String[] args) {
        System.out.println("Hello Gradle!");
    }
}

build.gradle: 的build.gradle:

group 'com.example'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'application'

mainClassName = "HelloGradle"

sourceCompatibility = 1.8

repositories {
    maven {
        url("https://plugins.gradle.org/m2/")
    }
}

task(runMain, dependsOn: 'classes', type: JavaExec) {
    main = 'com.example.HelloGradle'
    classpath = sourceSets.main.runtimeClasspath
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

When I click on the run task inside the Gradle Projects window, I get the following: 当我单击Gradle Projects窗口中的运行任务时,我得到以下内容:

在此输入图像描述

How can I setup either Gradle or IntelliJ IDEA to print the content in the main method to IntelliJ IDEA's Console view? 如何设置Gradle或IntelliJ IDEA以将主方法中的内容打印到IntelliJ IDEA的控制台视图?

Really don't understand why the Console view didn't pop up (from within IntelliJ IDEA and also why it doesn't show me what the error is)... 真的不明白为什么Console视图没有弹出(从IntelliJ IDEA内部,以及为什么它没有告诉我错误是什么)...

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. 要设置IntelliJ来运行主类,您只需右键单击HelloGradle类中的main()方法,然后从菜单中选择“Run HelloGradle.main()”。 You only do this once, because it will now show up on the top-right Run/Configuration menu together with other tasks (ie, Gradle tasks) you run. 您只需执行一次,因为它现在将显示在右上角的“运行/配置”菜单中,以及您运行的其他任务(即Gradle任务)。 Output should display in your console now. 输出现在应显示在您的控制台中。

For you gradle file, this is all you need to get all the Gradle tasks under Tasks->build->... running smoothly. 对于gradle文件,这就是在Tasks-> build - > ...下顺利运行所有Gradle任务所需的全部内容。

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
}

Just in case, don't forget to hit the "refresh" button, the top-left one in the Gradle projects view. 为了以防万一,不要忘记点击Gradle项目视图中左上角的“刷新”按钮。

UPDATE1 : I added the task portion in the Gradle file and it runs fine. UPDATE1 :我在Gradle文件中添加了task部分,运行正常。 You can run the project from the Gradle project->Run Configurations->HelloGradle[runMain]. 您可以从Gradle项目 - >运行配置 - > 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; 要查看输出,左下角的“运行”视图中有一个切换按钮,它的名称为“切换任务执行/文本模式”,上面带有“ab”图标; push it and you should see the output just the same. 推它,你应该看到输出相同。

UPDATE2 : Click the encircled button to see output. UPDATE2 :单击环绕按钮以查看输出。 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM