run main method using gradle "run" task

The easiest is probably to use application plugin. Add apply plugin: 'application' to your build.gradle and set mainClassName = com.bla.MainRunner . To add arguments to your main class modify the run task and edit the args property

run {
  args += 'first_arg'
}

Classpath is taken automatically from main sourceSet, if you want different one, you can edit classpath property of the run task.

If you need more customization, you can define your own task of type JavaExec like this

task myRun(type: JavaExec) {
  classpath sourceSets.main.runtimeClasspath
  main = "com.bla.MainRunner"
  args "arg1", "arg2"
}

task run(type: JavaExec) {
  group = 'Run' // <-- change the name as per your need
  description = 'Small description what this run will do'

  classpath sourceSets.main.runtimeClasspath // <-- Don't change this
  main = "com.mypackage.myclassNameContaingMainMethod"
  args "arg1", "arg2"
}

This is a independent registered task and can also have group and description and other properties of task.

Tags:

Java

Main

Gradle