Gradle task - pass arguments to Java application


I have a Java application that runs with a custom gradle task and the application requires some arguments upon being invoked. These are:

programName ( string | -f filename | -d key | -h)
    string         Message to be used.
    -d key         Use default messages, key must be s[hort], m[edium] or l[ong].
    -f filename    Use specified file as input.
    -h             Help dialog.

Gradle task looks like:

task run (type: JavaExec){
    description = "Secure algorythm testing"
    main = 'main.Test'
    classpath = sourceSets.main.runtimeClasspath

I've tried running gradle run -h and it does not work.

This question is tagged with java gradle arguments

~ Asked on 2014-12-22 14:06:57

The Best Answer is


Since Gradle 4.9, the command line arguments can be passed with --args. For example, if you want to launch the application with command line arguments foo --bar, you can use

gradle run --args='foo --bar'

See Also Gradle Application Plugin

How to upgrade Gradle wrapper

~ Answered on 2018-07-18 17:36:17


Gradle 4.9+

gradle run --args='arg1 arg2'

This assumes your build.gradle is configured with the Application plugin. Your build.gradle should look similar to this:

plugins {
  // Implicitly applies Java plugin
  id: 'application'

application {
  // URI of your main class/application's entry point (required)
  mainClassName = 'org.gradle.sample.Main'

Pre-Gradle 4.9

Include the following in your build.gradle:

run {
    if (project.hasProperty("appArgs")) {

Then to run: gradle run -PappArgs="['arg1', 'args2']"

~ Answered on 2015-04-01 04:03:01

Most Viewed Questions: