IDEA中使用Gradle运行main方法

IDEA中使用Gradle运行main方法

本文介绍了如何在IntelliJ IDEA中使用Gradle运行main方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

IntelliJ IDEA的新手(使用2017.1.3)和gradle ...

Am new to IntelliJ IDEA (using 2017.1.3) and gradle...

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'
}

当我点击Gradle Projects窗口中的运行任务,我得到以下结果:

When I click on the run task inside the Gradle Projects window, I get the following:

如何设置Gradle或IntelliJ IDEA以将主方法中的内容打印到IntelliJ IDEA的控制台视图?

How can I setup either Gradle or IntelliJ IDEA to print the content in the main method to IntelliJ IDEA's Console view?

真的不明白为什么Console视图没有弹出(来自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)...

推荐答案

要设置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.

这篇关于如何在IntelliJ IDEA中使用Gradle运行main方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:55