Search code examples
windowsgradlejavafxencodingcygwin

Encoding issue when running Gradle app distributions


I am developing this JavaFX app using Gradle in Eclipse. OS is Windows10 but I'm using Cygwin mostly...

In a cell in a TableView I have a String "référé".

It's written in Groovy. When I run the app using the Groovy Console from Eclipse the Stage is displayed and the String is displayed OK.

But when I do

$ ./gradlew installdist 

... and then run the app in the distribution directory, using either (Cygwin)

$ ./MyApp 

or (Windows Command Prompt)

D:\My Documents\software projects\operative\MyApp\1.0.0\bin>MyApp.bat

... the String is incorrectly displayed: the "é" characters are shown as black lozenges with white question marks in them.

In Cygwin I tried this:

$ cmd /c chcp 65001

response: "Active code page: 65001". But running the app after that still produced this encoding error. The trouble with an encoding problem like this is that I have no idea where to begin... does this tend to indicate that the Cygwin and Windows Command consoles are both using an "incorrect" encoding... which is somehow "infectious" for any app that is run from them?

How do I find their respective encodings... and how do I make the app run with UTF-8?

MCVE
build.gradle:

apply plugin: 'java-library'
apply plugin: 'groovy'
apply plugin: 'application'
mainClassName = 'core.TMApp'
String operativeDir = "D:\\My Documents\\software projects\\operative\\${name}"
String version = "1.0.0"
installDist{
    destinationDir = file( "$operativeDir/$version" )
}

compileGroovy { options.encoding = 'UTF-8' }


dependencies {
    api 'org.apache.commons:commons-math3:3.6.1'
    implementation 'com.google.guava:guava:23.0'
    testImplementation 'junit:junit:4.12'
    compile 'org.codehaus.groovy:groovy-all:2.5.3'
}

repositories {
    jcenter()
}

test.groovy:

package core
import javafx.application.Application
import javafx.event.*
import javafx.geometry.Insets
import javafx.geometry.Pos
import javafx.scene.Scene
import javafx.scene.control.*
import javafx.scene.control.cell.PropertyValueFactory
import javafx.scene.layout.*
import javafx.stage.Stage    

class TMApp extends Application {
    public static void main( args ) {
        // specific for Groovy JavaFX:
        TMApp.launch( TMApp, args )
    }

    public void start(Stage primaryStage) {
        BorderPane borderPane = new BorderPane()
        borderPane.minHeight = 400
        borderPane.minWidth = 600

        VBox centrePane = new VBox()
        TableView entryTable = new TableView()
        centrePane.children.addAll( entryTable )
        entryTable.columnResizePolicy = TableView.CONSTRAINED_RESIZE_POLICY
        TableColumn headwordColumn = new TableColumn("Headword")
        headwordColumn.cellValueFactory = new PropertyValueFactory("headword")
        headwordColumn.maxWidth = 150
        headwordColumn.minWidth = 150
        TableColumn defColumn = new TableColumn("Definition")
        defColumn.cellValueFactory = new PropertyValueFactory("definition")
        entryTable.getColumns().addAll(headwordColumn, defColumn)

        Entry entry = new Entry("référé", "blah blah blah\nglah glah glah\nvah vah vah")
        // Entry entry = new Entry("r�f�r�", "blah blah blah\nglah glah glah\nvah vah vah")
        // Entry entry = new Entry("r�f�r�", "blah blah blah\nglah glah glah\nvah vah vah")
        entryTable.getItems().add(entry)

        borderPane.setCenter( centrePane )

        Scene scene = new Scene( borderPane )
        primaryStage.setScene(scene)
        primaryStage.show()

    }
}


class Entry {
    private String headword
    private String definition
    public Entry(String headword, String definition) {
        this.headword = headword
        this.definition = definition
    }
    public String getHeadword() { return headword }
    public String getDefinition() { return definition }
}

Solution

  • Interesting... that didn't take long. I propose to leave this here as there don't seem to be other questions covering this issue.

    In Eclipse I put focus in the .groovy file and went Alt-Enter (File --> Properties).

    This showed a dialog with the first page showing called "Resource". At the bottom, under "Text file encoding" the radio button was set at "Default (inherited from container: Cp1252)".

    • Changed to the "Other" radio button and chose "UTF-8".
    • Apply and Close

    This messed up the String in question in the file itself (same weird bad encoding). When I re-entered "référé" correctly, and did another Gradle distribution, and ran the shell file (./MyApp), it worked OK: the encoding was correct.

    PS I have to wait 2 days before I can accept my own answer. Other answers may shine some unexpected light on this.