Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 show

Fix it fast

Most common cause, 1 of 4

System.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.

Fix
chcp 65001
java -Dstdout.encoding=UTF-8 -Dstderr.encoding=UTF-8 -jar app.jar
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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.

  • Applies to JDK 27, JDK 25 (LTS), JDK 21 (LTS), Windows Command Prompt
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
java -version
java -XshowSettings:properties -version 2>&1 | findstr encoding
chcp

Then find the line you see in this list and open its fix:

  1. 1System.out follows the console code page, not file.encodingfile.encoding = UTF-8, but stdout.encoding shows another code page and chcp reports 437, 850 or similar
  2. 2The program runs on JDK 17 or older, where Windows uses its code pagejava -version reports 17 or lower and file.encoding names a Windows code page charset rather than UTF-8
  3. 3The code calls System.setProperty for file.encoding at runtimeA main method or static block contains System.setProperty("file.encoding", "UTF-8") and nothing changes
  4. 4javac decodes the .java files with a different encoding than they were saved inRuntime 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 console
  • java -Dfile.encoding=UTF-8 changes nothing in the console output
  • Active 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.

Check
java -XshowSettings:properties -version 2>&1 | findstr stdout.encoding

Fix

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.

Fix
chcp 65001
java -Dstdout.encoding=UTF-8 -Dstderr.encoding=UTF-8 -jar app.jar

Verify

stdout.encoding now prints UTF-8 and accented text shows correctly in the console.

Verify
java -Dstdout.encoding=UTF-8 -XshowSettings:properties -version 2>&1 | findstr stdout.encoding

Confirmed by docs.oracle.com

Did this fix it?

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.

Check
java -XshowSettings:properties -version 2>&1 | findstr file.encoding

Fix

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.

Fix
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.

Verify
java -XshowSettings:properties -version 2>&1 | findstr file.encoding

Confirmed by openjdk.org

Did this fix it?

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.

Check
findstr /s /n /i "file.encoding" *.java

Fix

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.

Fix
// 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.

Verify
chcp 65001 && java -jar app.jar

Confirmed by openjdk.org

Did this fix it?

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.

Check
javac -encoding UTF-8 -d out src\Main.java

Fix

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.

Fix
<!-- 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.

Verify
mvn clean package

Confirmed by docs.oracle.com

Did this fix it?

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.

Which stack is behind this page?

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.