How to run main method using Gradle from IntelliJ IDEA?

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

Java file:

package com.example;

public class HelloGradle {

    public static void main(String[] args) {
        System.out.println("Hello 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 launch task inside the Gradle Projects window, I get this:

enter image description here

How can I configure either Gradle or IntelliJ IDEA to print content in the main method in the IntelliJ IDEA console view?

Really don't understand why the console view didn't show up (from IntelliJ IDEA and also why it doesn't show me what the error is) ...

+3


source to share


1 answer


To configure IntelliJ to run the main class, all you have to do is right-click on a method main()

in your class HelloGradle

and then select Run HelloGradle.main () from the menu. You only do this once, because now it will show up in the top right Run / Configuration menu along with the other tasks (i.e. Gradle tasks) that you run. The output should now be displayed in the console.

For you, the Gradle file is all you need to make all the Gradle tasks under Tasks-> Build β†’ ... run 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
}

      



Just in case, don't forget to click the Update button, top left in the Gradle Projects View.

Update1 : I added the part task

to the Gradle file and it works fine. You can run the project from Gradle Project-> Run Configurations-> HelloGradle [runMain]. To see the result, in the Run view at the bottom left there is a toggle button called Toggle Task / Text Mode with an "ab" icon; click it and you should see the output is the same.

UPDATE2 : Press the circle button to see the result.enter image description here

+4


source







All Articles