Ad

Java Gradle Eclipse - UTF-8 Symbols Showing Before Build In IDE But Not After Build

- 1 answer

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. some of the strings in there use UTF-8 symbols/emojis. I managed to get them into my java objects with "Jackson" and by using the getBytes() method for the strings. When i generate the HTML from those objects in eclipse they show up the right way (when opened in browser outside of 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). Everything works fine, besides the UTF-8 symbols not showing, instead i get a "?", like the encoding is not set right. So i think the problem is encoding settings in gradle.

What i tried so far is adding this to gradlew.bat:

set GRADLE_OPTS=-Dfile.encoding=utf-8

this to build.gradle :

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

and this to gradle.Properties

systemProp.file.encoding=utf-8

like mentioned in this thread Show UTF-8 text properly in Gradle

None of those seem to help. Any ideas? Im stuck, struggle to find a solution.

Ad

Answer

-Dfile.encoding=UTF-8 sets the default encoding used at runtime when running your compiled code, e.g. 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 ....

To avoid to set it on the command line, you can set it also programmatically via System.setProperty("file.encoding", "UTF-8");. But better set the encoding where it used, e.g. instead of BufferedWriter writer= Files.newBufferedWriter(path);use BufferedWriter writer= Files.newBufferedWriter(path, StandardCharsets.UTF_8);.

Ad
source: stackoverflow.com
Ad