Passing an encoding switch to the JVM for a Gradle "JavaExec" task
Asked Answered
S

1

1

I am having a few encoding problems here.

This is on a W10 OS but I'm using both the DOS console and the Cygwin shell to run tasks directly. When in Eclipse I'm running using the Buildship plugin, and for my "production" output I'm making a "fat" jar (i.e. containing all the dependency jars).

To get the fat jar to run in a DOS console I go like this:

> chcp 65001
> java -jar -Dfile.encoding=UTF-8 myFatJar.jar

To get the fat jar to run in a Cygwin console I omit the "chcp" command

... and both of these run fine, no encoding probs.

I have a run task that looks like this:

task myRun(type: JavaExec, dependsOn: assemble ) {
    standardInput = System.in
    classpath sourceSets.main.runtimeClasspath
    main = "core.ConsoleHandler"
}

When I run this in Eclipse (Buildship plugin) there are no encoding probs.

When, in the gradle dir, I go like this:

> gradle myRun

I get encoding problems whether I'm using a DOS or Cygwin console: problems with Unicode chars going to System.out.

In my gradle.build (I'm using the application plugin) I tried this:

applicationDefaultJvmArgs = ["-Dfile.encoding=UTF-8"]

... but this doesn't seem to have any effect.

Incidentally, during testing, I have some output going to System.out... and this comes out fine when I examine the JUnit/Mockito test results.

In my quest to get "all my ducks in a row" I also tried messing with the Java code itself: instead of using System.out directly I tried this:

    PrintStream out = new PrintStream(System.out, true, "UTF-8");
    out.print( message );

... but this then caused encoding problems with output to System.out when running tests!

The gradle command applicationDefaultJvmArgs makes me wonder if there is a way of passing "non-default" switches to the JVM when runing a JavaExec-type task...

ever so slightly later

Hah.. such is the way with these things:

I added the following line to my myRun task:

jvmArgs "-Dfile.encoding=UTF-8" 

... and the encoding problem with the DOS and Cygwin console runs was... solved! However, when I run the myRun task in Eclipse Buildship I now get encoding issues!:

référé juge des ~s: "Judge sitting in Chambers to deal with urgent matters"
état juge de mise en ~: joinder of issue judge

'é' should be 'é' (and was before adding this jvmArgs argument)

On the whole of course this is an improvement... but it'd be nice to know from an expert what's going on here...

Stypsis answered 11/6, 2017 at 11:1 Comment(0)
S
0

Sometimes things do make sense... though I still find encoding a harrowing, intimidating and soul-destroying subject.

My Eclipse "run configuration" for myRun was lacking this JVM argument. Adding it was simple enough: Run --> Run configurations --> choose "myRun" --> Arguments tab --> JVM Arguments.

All my ducks appear to be in a UTF-8 row. Hurrah.

Stypsis answered 15/6, 2017 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.