run main method using gradle "run" task
Solution 1
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"
}
Solution 2
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.
Comments
-
Elad Benda over 4 years
I want to run my
main
method via gradle taskThis is how I run via the cmd:
java -cp RTMonitor.jar com.bla.MainRunner first_arg
how should it be written in gradle?
run { args += ['java -cp RTMonitor.jar com.bla.MainRunner first_arg'] }
Update
I have tried
task myRun(type: JavaExec) { classpath configurations.main main = "com.bla.runners.StatsLogGenerator" args "arg1", "arg2" }
and I got:
Error:(71, 0) Could not find property 'main' on configuration container.
the I have tried: task myRun(type: JavaExec) { classpath "configurations.main" main = "com.bla.runners.StatsLogGenerator" args "arg1", "arg2" }
and i got an error:
FAILURE: Build failed with an exception. 17:49:21.855 [ERROR] [org.gradle.BuildExceptionReporter] 17:49:21.856 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong: 17:49:21.856 [ERROR] [org.gradle.BuildExceptionReporter] Execution failed for task ':myRun'. 17:49:21.856 [ERROR] [org.gradle.BuildExceptionReporter] > Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1 17:49:21.864 [ERROR] [org.gradle.BuildExceptionReporter] 17:49:21.865 [ERROR] [org.gradle.BuildExceptionReporter] * Exception is: 17:49:21.866 [ERROR] [org.gradle.BuildExceptionReporter] org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':myRun'. 17:49:21.867 [ERROR] [org.gradle.BuildExceptionReporter] at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69) 17:49:21.882 [ERROR] [org.gradle.BuildExceptionReporter] at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:361) 17:49:21.882 [ERROR] [org.gradle.BuildExceptionReporter] at org.gradle.process.internal.DefaultJavaExecAction.execute(DefaultJavaExecAction.java:31)
but when I run via Intellij, every thig works OK
-
Elad Benda over 9 yearsand what if I have two main methods, and I want to define a different task for each?
mainClassName
is assign in the global scope? -
Tibor Blenessy over 9 yearsthen you will be likely better of defining your own task of type JavaExec
-
Elad Benda over 9 yearswhere is the doc to see which properties each task type has?
-
Tibor Blenessy over 9 yearsIn the DSL Reference guide. For JavaExec here gradle.org/docs/current/dsl/org.gradle.api.tasks.JavaExec.html
-
Elad Benda over 9 years
Error:(71, 0) Could not find property 'main' on configuration container.
-
Tibor Blenessy over 9 yearssorry, my error, classpath should be something like
sourceSets.main.runtimeClasspath