运行Gradle应用分发时的编码问题

时间:2018-12-29 16:46:48

标签: windows gradle javafx encoding cygwin

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

TableView的一个单元格中,我有一个String“référé”。

它是用Groovy编写的。当我从Eclipse使用Groovy控制台运行应用程序时,将显示Stage,并显示String很好。

但是当我这样做

$ ./gradlew installdist 

...,然后使用(Cygwin)

在分发目录中运行应用程序
$ ./MyApp 

或(Windows命令提示符)

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

... String显示不正确:“é”字符显示为黑色菱形,其中带有白色问号。

在Cygwin中,我尝试了以下方法:

$ cmd /c chcp 65001

响应:“活动代码页:65001”。但是在那之后运行应用仍然会产生此编码错误。像这样的编码问题带来的麻烦是我不知道从哪里开始...这是否表明Cygwin和Windows Command控制台都使用了“不正确”的编码...从他们那里运行的任何应用程序?

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

1 个答案:

答案 0 :(得分:0)

有趣的...没多久。我建议将其保留在此处,因为似乎没有其他问题可以解决此问题。

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

这显示了一个对话框,第一页显示为“资源”。在底部的“文本文件编码”下,单选按钮设置为“默认值(从容器继承:Cp1252)”。

  • 更改为“其他”单选按钮,然后选择“ UTF-8”。
  • 应用并关闭

这使文件本身中的String混乱(同样奇怪的错误编码)。当我正确地重新输入“référé”,并进行了另一次Gradle分发,并运行了外壳文件(./MyApp)时,它工作正常:编码正确。

PS,我必须等待2天才能接受自己的答案。其他答案可能会为此带来一些意想不到的启示。