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

标签 windows gradle javafx encoding cygwin

我正在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

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

在Cygwin中,我尝试了以下操作:
$ cmd /c chcp 65001

响应:“ Activity 代码页: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 }
}

最佳答案

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

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

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

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

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

    附言:我必须等待2天才能接受自己的答案。其他答案可能会对此产生一些意外的启发。

    关于windows - 运行Gradle应用分发时的编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53971437/

    相关文章:

    java - 如何在单击按钮时打开 TilePane 中列出的图像

    java - en_EN 是有效的语言环境吗

    windows - 获取远程服务器环境变量

    Python DNS 服务器

    java - 如何从我的资源构建路径加载 ResourceBundle?

    android-studio - 为什么Android Studio默认使用Gradle 2.14.1而不是最新版本?

    linux - 从 Linux 设置一个 Windows exe 图标(没有 Wine?)

    c++ - 有没有办法让 C++ 控制台文本变大?

    java - 在 Android Studio 中集成 Realm 时出错

    Javafx 使用线程将子项动态添加到 TreeView ?