运行 Gradle 应用分发时的编码问题
Encoding issue when running Gradle app distributions
我正在 Eclipse 中使用 Gradle 开发这个 JavaFX 应用程序。 OS 是 Windows10 但我主要使用 Cygwin...
在 TableView
的单元格中,我有一个 String
"référé"。
写在Groovy。当我 运行 使用来自 Eclipse 的 Groovy 控制台的应用程序时,显示 Stage
并且 String
显示正常。
但是当我这样做时
$ ./gradlew installdist
... 然后 运行 分发目录中的应用程序,使用 (Cygwin)
$ ./MyApp
或(Windows命令提示符)
D:\My Documents\software projects\operative\MyApp.0.0\bin>MyApp.bat
... String
显示不正确:“é”字符显示为带有白色问号的黑色菱形。
在 Cygwin 中我试过这个:
$ cmd /c chcp 65001
响应:"Active code page: 65001"。但是运行之后应用程序仍然产生了这个编码错误。像这样的编码问题的麻烦在于我不知道从哪里开始......这是否表明 Cygwin 和 Windows 命令控制台都在使用 "incorrect" 编码......这不知何故 "infectious" 对于来自他们的 运行 的任何应用程序?
如何找到它们各自的编码...以及如何使用 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(文件 --> 属性)。
这显示了一个对话框,第一页显示名为 "Resource"。在底部 "Text file encoding" 下,单选按钮设置为 "Default (inherited from container: Cp1252)"。
- 更改为 "Other" 单选按钮并选择 "UTF-8"。
- 申请并关闭
这弄乱了文件本身中的问题 String
(同样奇怪的错误编码)。当我重新正确输入 "référé",并执行另一个 Gradle 分发,以及 运行 shell 文件 (./MyApp) 时,它工作正常:编码是正确的。
PS我要等2天才能接受我自己的回答。其他答案可能对此有一些意想不到的启发。
我正在 Eclipse 中使用 Gradle 开发这个 JavaFX 应用程序。 OS 是 Windows10 但我主要使用 Cygwin...
在 TableView
的单元格中,我有一个 String
"référé"。
写在Groovy。当我 运行 使用来自 Eclipse 的 Groovy 控制台的应用程序时,显示 Stage
并且 String
显示正常。
但是当我这样做时
$ ./gradlew installdist
... 然后 运行 分发目录中的应用程序,使用 (Cygwin)
$ ./MyApp
或(Windows命令提示符)
D:\My Documents\software projects\operative\MyApp.0.0\bin>MyApp.bat
... String
显示不正确:“é”字符显示为带有白色问号的黑色菱形。
在 Cygwin 中我试过这个:
$ cmd /c chcp 65001
响应:"Active code page: 65001"。但是运行之后应用程序仍然产生了这个编码错误。像这样的编码问题的麻烦在于我不知道从哪里开始......这是否表明 Cygwin 和 Windows 命令控制台都在使用 "incorrect" 编码......这不知何故 "infectious" 对于来自他们的 运行 的任何应用程序?
如何找到它们各自的编码...以及如何使用 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(文件 --> 属性)。
这显示了一个对话框,第一页显示名为 "Resource"。在底部 "Text file encoding" 下,单选按钮设置为 "Default (inherited from container: Cp1252)"。
- 更改为 "Other" 单选按钮并选择 "UTF-8"。
- 申请并关闭
这弄乱了文件本身中的问题 String
(同样奇怪的错误编码)。当我重新正确输入 "référé",并执行另一个 Gradle 分发,以及 运行 shell 文件 (./MyApp) 时,它工作正常:编码是正确的。
PS我要等2天才能接受我自己的回答。其他答案可能对此有一些意想不到的启发。