How to pass "-parameters" option to javac compiler, when building my java project with gradle?

How to pass “-parameters” option to javac compiler, when building my java project with gradle? I have asked this on stackoverflow [*], but no response. Hopefully I’ll get help here…


Here’s some ideas:


I’ve used this (below) in build.gradle

ext {
  mainClass = "com.jthad.Stamper"
  task execute(type:JavaExec) {
  main = mainClass
  jvmArgs = ["-Djava.awt.headless=true"]
  if (project.hasProperty("appArgs")) {
  classpath = sourceSets.main.runtimeClasspath

and run it from the command line with

gradle -PmainClass=com.jthad.Stamper -PappArgs="['multipage0.tif']" execute

Thanks, but that is not answering my question.

I would like to use the new Java 8 feature to obtain the names of method parameters, as described here:

To use this new feature, I need to pass the “-parameters” option to the jacac compiler at compile time. (Please note, this is not the same as providing system properties to the JVM runtime environment).

I tried following this suggestion:

but it did not work for me. Any other suggestions how to use this new Java 8 feature with gradle?

Does this SO posting help? .

Thanks, but it did not help. I have tried adding each of the below in my build.gradle file, but non works :frowning: Any other suggestions? I’m sure there must be a way to pass options to the javac compiler with Gradle.

 compileJava {
     options.compilerArgs << '-parameters'
     options.fork = true
     options.forkOptions.executable = 'javac'  
tasks.withType(JavaCompile) {
    options.fork = true
    options.forkOptions.jvmArgs += ["-parameters"]  

tasks.withType(JavaCompile) {
    options.fork = true
    options.compilerArgs += ["-parameters"]  
1 Like

What I’m trying to do is make the below code to output:


If I enable the following option in eclipse it works:

“Store information about method parameters (usable via reflection)”

(Window -> Preferences -> Java -> Compiler) So I know it is possible. But how can I do this with Gradle, as I don’t want to depend on my IDE?

 import java.lang.reflect.Method;  
 import java.lang.reflect.Parameter;

 public class ParamPlay {

       public static void main(String[] args) throws Exception {
             Method m = MyInterface.class.getMethod("method", String.class);
             Parameter p = m.getParameters()[0];

        public interface MyInterface {
             String method(String str);
1 Like