简体   繁体   English

Java Gradle Eclipse - UTF-8 符号在 IDE 构建之前显示,但在构建之后不显示

[英]Java Gradle Eclipse - UTF-8 symbols showing before build in IDE but not after build

i'm fairly new to coding and currently im trying to build my first fat JAR with gradle in eclipse. My project generates a HTML file with data i get from a JSON file.我对编码相当陌生,目前我正在尝试在 eclipse 中使用 gradle 构建我的第一个胖子 JAR。我的项目生成一个 HTML 文件,其中包含我从 JSON 文件中获取的数据。 some of the strings in there use UTF-8 symbols/emojis.那里的一些字符串使用 UTF-8 符号/表情符号。 I managed to get them into my java objects with "Jackson" and by using the getBytes() method for the strings.我设法使用“Jackson”将它们放入我的 java 对象中,并通过对字符串使用 getBytes() 方法。 When i generate the HTML from those objects in eclipse they show up the right way (when opened in browser outside of eclipse).当我从 eclipse 中的那些对象生成 HTML 时,它们以正确的方式显示(在 eclipse 之外的浏览器中打开时)。 The problem occurs, when i build the fat jar with "gradlew jar" (i had to make some changes in build.gradle to jar to make it contain the dependencies).当我用“gradlew jar”构建胖子 jar 时出现问题(我必须在 build.gradle 中进行一些更改以使其包含依赖项 jar)。 Everything works fine, besides the UTF-8 symbols not showing, instead i get a "?", like the encoding is not set right.一切正常,除了未显示 UTF-8 符号外,我得到一个“?”,就像编码设置不正确一样。 So i think the problem is encoding settings in gradle.所以我认为问题是 gradle 中的编码设置。

What i tried so far is adding this to gradlew.bat:到目前为止我尝试的是将其添加到 gradlew.bat:

set GRADLE_OPTS=-Dfile.encoding=utf-8

this to build.gradle:这到 build.gradle:

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

and this to gradle.Properties这是 gradle.Properties

systemProp.file.encoding=utf-8

like mentioned in this thread Show UTF-8 text properly in Gradle就像这个线程中提到的那样 在Gradle 中正确显示 UTF-8 文本

None of those seem to help.这些似乎都没有帮助。 Any ideas?有任何想法吗? Im stuck, struggle to find a solution.我卡住了,很难找到解决方案。

-Dfile.encoding=UTF-8 sets the default encoding used at runtime when running your compiled code, eg java -Dfile.encoding=UTF-8 -jar... . -Dfile.encoding=UTF-8设置运行编译代码时在运行时使用的默认编码,例如java -Dfile.encoding=UTF-8 -jar... Don't mix it up with the encoding that is used by the Java compiler to read the .java files: javac -encoding... .不要将它与 Java 编译器用来读取.java文件的编码混淆: javac -encoding...

To avoid to set it on the command line, you can set it also programmatically via System.setProperty("file.encoding", "UTF-8");为避免在命令行上设置它,您也可以通过System.setProperty("file.encoding", "UTF-8");编程方式设置它。 . . But better set the encoding where it used, eg instead of BufferedWriter writer= Files.newBufferedWriter(path);但是最好在它使用的地方设置编码,例如而不是BufferedWriter writer= Files.newBufferedWriter(path); use BufferedWriter writer= Files.newBufferedWriter(path, StandardCharsets.UTF_8); . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM