Java on Windows still shows or reads text in the wrong encoding
Your error
System.out.println prints ? or é instead of é in the Windows console2 more lines it can showFix it fast
Most common cause, 1 of 4System.out follows the console code page, not file.encoding
Switch the console to UTF-8 before starting Java, or start the JVM with stdout.encoding set to UTF-8, the only command-line value the Java docs support for it.
chcp 65001
java -Dstdout.encoding=UTF-8 -Dstderr.encoding=UTF-8 -jar app.jarShort answer
Since JDK 18 the default charset is UTF-8, yet Windows console output can stay garbled because System.out follows stdout.encoding, taken from the console code page. Calling System.setProperty for file.encoding never works, and javac decodes sources by its own setting. Run chcp 65001 or start java with -Dstdout.encoding=UTF-8, and compile with -encoding UTF-8. Applies to JDK 21, 25 and 27.
Which cause do you have?
Print the encoding properties of the JDK you actually run, then the console code page. file.encoding not UTF-8 points to cause 2. file.encoding UTF-8 but stdout.encoding something else points to cause 1. Both UTF-8 with string literals still wrong points to cause 4.
java -version
java -XshowSettings:properties -version 2>&1 | findstr encoding
chcpThen find the line you see in this list and open its fix:
- 1System.out follows the console code page, not file.encoding
file.encoding = UTF-8, but stdout.encoding shows another code page and chcp reports 437, 850 or similar - 2The program runs on JDK 17 or older, where Windows uses its code page
java -version reports 17 or lower and file.encoding names a Windows code page charset rather than UTF-8 - 3The code calls System.setProperty for file.encoding at runtime
A main method or static block contains System.setProperty("file.encoding", "UTF-8") and nothing changes - 4javac decodes the .java files with a different encoding than they were saved in
Runtime properties are all UTF-8, but string literals with accents are wrong in the compiled program
What does the error look like?
System.out.println prints ? or é instead of é in the Windows consolejava -Dfile.encoding=UTF-8 changes nothing in the console outputActive code page: 437
Cause 1 of 4
System.out follows the console code page, not file.encoding
You seefile.encoding = UTF-8, but stdout.encoding shows another code page and chcp reports 437, 850 or similar
Check
Garbled console output on JDK 18 and later usually comes from stdout.encoding, not from the default charset. The Java SE documentation says System.out converts characters with the stdout.encoding charset, which follows the host environment, so -Dfile.encoding=UTF-8 leaves it untouched.
java -XshowSettings:properties -version 2>&1 | findstr stdout.encodingFix
Switch the console to UTF-8 before starting Java, or start the JVM with stdout.encoding set to UTF-8, the only command-line value the Java docs support for it. Programs launched after chcp use the new code page; a TrueType console font is needed to draw the characters.
chcp 65001
java -Dstdout.encoding=UTF-8 -Dstderr.encoding=UTF-8 -jar app.jarVerify
stdout.encoding now prints UTF-8 and accented text shows correctly in the console.
java -Dstdout.encoding=UTF-8 -XshowSettings:properties -version 2>&1 | findstr stdout.encodingConfirmed by docs.oracle.com
Cause 2 of 4
The program runs on JDK 17 or older, where Windows uses its code page
You seejava -version reports 17 or lower and file.encoding names a Windows code page charset rather than UTF-8
Check
In JDK 17 and earlier the default charset depends on the locale; on Windows it is a code page charset such as windows-1252. JEP 400 also notes that setting file.encoding on the command line was never a supported way to change it on those releases, so results vary.
java -XshowSettings:properties -version 2>&1 | findstr file.encodingFix
Run the program on a current JDK, where UTF-8 is the default charset. Where you must stay on an old release, pass the charset explicitly to every reader and writer instead of relying on the default.
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
String text = Files.readString(Path.of("data.txt"), StandardCharsets.UTF_8);
Files.writeString(Path.of("out.txt"), text, StandardCharsets.UTF_8);Verify
file.encoding reports UTF-8 on the new JDK, and files written by the program open correctly in a UTF-8 editor.
java -XshowSettings:properties -version 2>&1 | findstr file.encodingConfirmed by openjdk.org
Cause 3 of 4
The code calls System.setProperty for file.encoding at runtime
You seeA main method or static block contains System.setProperty("file.encoding", "UTF-8") and nothing changes
Check
The default charset is fixed when the JVM starts. JEP 400 states plainly that setting file.encoding programmatically after startup does not work, so the line has no effect on readers, writers or System.out.
findstr /s /n /i "file.encoding" *.javaFix
Delete the setProperty call. Pass StandardCharsets.UTF_8 where the code reads or writes text, and set stdout.encoding on the command line if console output is the problem.
// remove: System.setProperty("file.encoding", "UTF-8");
var out = new java.io.PrintStream(System.out, true, java.nio.charset.StandardCharsets.UTF_8);
out.println("é ñ ü");Verify
The text prints correctly in a console set to code page 65001 without the setProperty line.
chcp 65001 && java -jar app.jarConfirmed by openjdk.org
Cause 4 of 4
javac decodes the .java files with a different encoding than they were saved in
You seeRuntime properties are all UTF-8, but string literals with accents are wrong in the compiled program
Check
javac assumes .java files use the default charset unless -encoding is given. On JDK 18 and later that means UTF-8, so a source file saved as windows-1252 has its non-ASCII literals misread, and on older JDKs the opposite mismatch happens.
javac -encoding UTF-8 -d out src\Main.javaFix
Save the sources as UTF-8 and name the encoding explicitly in every build. For Maven, set project.build.sourceEncoding; for plain javac, pass -encoding.
<!-- pom.xml -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>Verify
After a clean rebuild, the literals print correctly in a UTF-8 console.
mvn clean packageConfirmed by docs.oracle.com
How do I stop it coming back?
- Pass a Charset to every Files, Reader and Writer call that handles text, so no code depends on the platform default.
- Set project.build.sourceEncoding to UTF-8 in every Maven project and keep editors saving .java files as UTF-8.
- Put -Dstdout.encoding=UTF-8 in the start script of console tools that print non-English text on Windows.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.