1

I have a Java project with gradle. This project has a main method.

I know I can run the main method using ./gradlew run but I cannot pass args to the Java main method using this function

I want to create a single-word bash script like ./runMyCode that can accept args such as ./runMyCode hello.txt -f which will be eventually be passed to the java application.

However I am unable to do this. PLease help me...

3
  • I think this is what you are looking for. Commented Mar 25, 2018 at 9:25
  • I kinda do not want to use the Pargs=... or somehow disguise that into a bash script ? Commented Mar 25, 2018 at 9:43
  • You definitely can hide it in bash script. #!/bin/bash function join_by { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; } args=$(join_by "', '" $@) gradlew run -PappArgs="[\'$args\']" ./myscript arg1 arg2 will call the following command: gradlew run -PappArgs="['arg1', 'arg2']" Note that this approach requires the specific run task in your .gradle file. Commented Mar 25, 2018 at 11:23

1 Answer 1

0

You could build an executable jar with gradle. Then your runMyCode script would look like this:

#!/bin/bash

java -jar my-code.jar "$@"

And you can use as you wished: ./runMyCode hello.txt -f.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.