0

I would like to set javac compile option as "-J-Duser.language=en" in gradle.

my question is pretty mach same as Here

so i tried following code:

project.tasks.withType(JavaCompile.class) {
    options.fork = true
    options.forkOptions.jvmArgs = ["-J-Duser.language=en"]
    ...
}

Then gradle return following error message

Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Unrecognized option: -J-Duser.language=en

I believe that I am doing something wrong. How can I change the output to English in gradle plugin?

ADD: I also tried following code

project.tasks.withType(JavaCompile.class) {
    options.compilerArgs << '-J-Duser.language=en'
    ... 
}

then, gradle returns this error code:

    > Cannot specify -J flags via `CompileOptions.compilerArgs`. 
Use the `CompileOptions.forkOptions.jvmArgs` property instead.
e.ume
  • 1
  • 1
  • I don't use gradle, but you are putting the _compiler_ option `-J-Duser.language=en` to something called "jvmArgs". That doesn't look right. The JVM option is just `-Duser.language=en`, or you can set [`compileArgs`](https://stackoverflow.com/questions/29593500/how-can-i-set-the-compileoptions-for-my-gradle-java-plugin) instead. – Sweeper Sep 06 '21 at 04:40
  • @Sweeper Thanks for responding. As I add some information the question, using options.compilerArgs did not work. and error message suggest to use forkOptions.jvmArgs. Do I missed something else? – e.ume Sep 06 '21 at 05:43
  • How about just specifying `-Duser.language=en`, without `-J`, as `jvmArgs`? – Sweeper Sep 06 '21 at 05:45
  • @Sweep thanks again. it worked! would you write an answer? or should I write down an answer? – e.ume Sep 06 '21 at 06:45

1 Answers1

0

What the -J option for the Java compiler does, is it sends the option after it to the JVM (source):

-J_option_

Passes option to the Java Virtual Machine (JVM), where option is one of the options described on the reference page for the Java launcher. For example, -J-Xms48m sets the startup memory to 48 MB.

But here you are passing JVM options, not compiler options, so you don't need the -J, and can just pass -Duser.language=en directly. As you have found out, Gradle even disallows you to pass JVM options indirectly via -J.

Sweeper
  • 213,210
  • 22
  • 193
  • 313