首页 > 解决方案 > 运行 Gradle 应用程序分发时的编码问题

问题描述

我正在使用 Eclipse 中的 Gradle 开发这个 JavaFX 应用程序。操作系统是 Windows10,但我主要使用 Cygwin ......

在一个牢房里,TableView我有一个String“référé”。

它是用 Groovy 编写的。当我使用 Eclipse 中的 Groovy 控制台运行应用程序时,Stage会显示并String显示 OK。

但是当我这样做时

$ ./gradlew installdist 

...然后使用(Cygwin)在分发目录中运行该应用程序

$ ./MyApp 

或(Windows 命令提示符)

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

... theString显示不正确:“é”字符显示为带有白色问号的黑色菱形。

在 Cygwin 我试过这个:

$ cmd /c chcp 65001

响应:“活动代码页:65001”。但之后运行该应用程序仍然会产生此编码错误。像这样的编码问题的麻烦在于我不知道从哪里开始......这是否往往表明 Cygwin 和 Windows 命令控制台都使用“不正确”的编码......这在某种程度上是“传染性的”从他们运行的任何应用程序?

如何找到它们各自的编码...以及如何使应用程序以 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()
}

测试.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 }
}

标签: windowsgradlejavafxencodingcygwin

解决方案


有趣……没多久。我建议将其留在这里,因为似乎没有其他问题涉及此问题。

在 Eclipse 中,我将焦点放在 .groovy 文件中,然后按 Alt-Enter(文件 --> 属性)。

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.


推荐阅读