In Java, console.charset() returns the java.nio.charset.Charset the console uses to encode the text you print and decode the keys the user types. It describes the terminal itself (keyboard and display), as set up by the operating system or the user.
It matters because the console charset is not always the same as Charset.defaultCharset(). On Linux and macOS both are usually UTF-8. On Windows, Command Prompt often still uses an older code page such as 437, while Java 18 and later use UTF-8 as the default charset for files.
An instance method of the java.io.Console class, available since Java 17. You never create a Console yourself: get it with Console console = System.console(); after import java.io.Console;, and check it for null before calling console.charset(...).
Requires Java 17 or later.
charset() was added in Java 17, so the examples on this page do not compile on Java 8 to 16. There, Charset.defaultCharset() or System.getProperty("file.encoding") is the closest substitute, but on Windows they usually describe the file encoding, not the console code page. Java 19+ also sets the stdout.encoding property, which normally matches the console when output goes to a terminal.
Syntax
Call it on the Console object; it takes no arguments:
Java
Console console = System.console();
if (console != null) {
Charset cs = console.charset(); // e.g. UTF-8
String name = cs.name(); // "UTF-8"
}
Call
What it does
console.charset()
Returns the Charset the terminal uses.
console.charset().name()
Canonical name, such as UTF-8 or IBM437.
console.charset().newEncoder().canEncode(s)
Checks whether the terminal charset can represent the text s.
Charset.defaultCharset()
The JVM default charset, which may differ from the console charset.
Added in Java 17; the Console class itself has existed since Java 6. The value comes from the host environment, so the same program can print different names on different computers.
Library declarations
Java
// java.io.Console (Java 17+)
public Charset charset()
Parameters
Parameter
Description
None
charset() takes no parameters.
Return Value
Return Value
Description
Charset
The charset used for console input and output. It may not be the same as Charset.defaultCharset().
Compare with Similar Methods
Ways to find out about text encoding:
Method
How it differs
Charset.defaultCharset()
JVM default for files when no charset is given; UTF-8 by default since Java 18.
System.getProperty("stdout.encoding")
Java 19+ property: the encoding used by System.out.
System.getProperty("native.encoding")
Java 17+ property: the operating system's own default encoding.
The PrintWriter that encodes output with the console charset.
Java Examples
Example 1 — Compare the Console Charset with the Default Charset
Print both charsets and check whether they are equal. The answer depends on the operating system and terminal settings.
Java
import java.io.Console;
import java.nio.charset.Charset;
public class ConsoleCharsetShow {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
Charset cs = console.charset();
Charset def = Charset.defaultCharset();
console.printf("Console charset: %s%n", cs.name());
console.printf("Default charset: %s%n", def.name());
console.printf("Same as default? %b%n", cs.equals(def));
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleCharsetShow. The first part assumes Java 18+ in a UTF-8 terminal on Linux or macOS. The Windows part is typical for code page 437 with Java 18+; your code page may differ.
Console charset: UTF-8
Default charset: UTF-8
Same as default? true
--- Windows Command Prompt using code page 437 ---
Console charset: IBM437
Default charset: UTF-8
Same as default? false
How It Works
Two charsets.console.charset() asks the terminal, while Charset.defaultCharset() asks the JVM for its file default.
Canonical names.name() prints the official name of each charset, so code page 437 shows up as IBM437.
UTF-8 terminal. On a typical Linux or macOS terminal with Java 18+, both are UTF-8 and equals returns true.
Windows mismatch. Command Prompt with code page 437 reports IBM437 for the console, while the JVM default is UTF-8, so the check prints false.
Example 2 — Check Which Characters the Terminal Can Show
A CharsetEncoder from the console charset reports whether a character can be represented. The source code uses \u escapes, so it stays plain ASCII.
Java
import java.io.Console;
import java.nio.charset.CharsetEncoder;
public class ConsoleCharsetCanEncode {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
CharsetEncoder encoder = console.charset().newEncoder();
String[] names = {"letter A", "e with acute", "check mark", "euro sign"};
String[] samples = {"A", "\u00e9", "\u2713", "\u20ac"};
console.printf("Terminal charset: %s%n", console.charset().name());
for (int i = 0; i < names.length; i++) {
String answer = encoder.canEncode(samples[i]) ? "yes" : "no";
console.printf("%-13s %s%n", names[i], answer);
}
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleCharsetCanEncode. The first part assumes a UTF-8 terminal (typical on Linux and macOS); the second part is typical for Windows Command Prompt with code page 437.
Terminal charset: UTF-8
letter A yes
e with acute yes
check mark yes
euro sign yes
--- Windows Command Prompt using code page 437 ---
Terminal charset: IBM437
letter A yes
e with acute yes
check mark no
euro sign no
How It Works
An encoder for the terminal.newEncoder() creates a CharsetEncoder that knows exactly which characters the console charset can turn into bytes.
Escapes keep the file ASCII."\u00e9", "\u2713", and "\u20ac" are the e-acute, check mark, and euro sign, written with Unicode escapes instead of the symbols themselves.
UTF-8 covers everything. UTF-8 can encode every Unicode character, so all four answers are yes.
Code page 437 is limited. It contains A and e-acute but has no check mark and no euro sign, so those two answers are no. Printing them anyway would show replacement characters.
Example 3 — Print charset() Next to the Encoding System Properties
Compare the console charset with the system properties you might use on older Java versions. getProperty with a default avoids printing null for missing properties.
Java
import java.io.Console;
public class ConsoleCharsetProperties {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
console.printf("console.charset(): %s%n", console.charset().name());
console.printf("stdout.encoding: %s%n", System.getProperty("stdout.encoding", "(not set)"));
console.printf("native.encoding: %s%n", System.getProperty("native.encoding", "(not set)"));
console.printf("file.encoding: %s%n", System.getProperty("file.encoding", "(not set)"));
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleCharsetProperties. The last part is a run where System.console() is null (IDE, redirected input). The terminal part assumes Java 21 in a UTF-8 terminal on Linux or macOS.
console.charset(): UTF-8
stdout.encoding: UTF-8
native.encoding: UTF-8
file.encoding: UTF-8
--- When System.console() returns null ---
No console available. Run this program from a terminal.
How It Works
The Java 17 answer. The first line asks the console directly, which is the reliable source on Java 17 and later.
Properties with a fallback.System.getProperty(key, "(not set)") returns the second argument when a property does not exist, for example stdout.encoding on Java versions before 19.
Three different meanings.stdout.encoding is what System.out uses, native.encoding is the operating system default, and file.encoding is the JVM default for files.
All agree here. In a UTF-8 terminal on Linux or macOS with a recent JDK every value is UTF-8; on Windows the console and stdout.encoding lines often show a code page instead.
Frequently Asked Questions
The Charset the console uses to read typed text and write printed text, for example UTF-8 on most Linux and macOS terminals or IBM437 in a Windows Command Prompt using code page 437.
Java 17. The Console class itself has existed since Java 6.
The default charset is used for files and is UTF-8 by default since Java 18. The console charset follows the terminal, and Windows terminals often still use a legacy code page such as 437.
There is no charset() method before Java 17. Charset.defaultCharset() and System.getProperty("file.encoding") are common guesses, but on Windows they usually report the file encoding rather than the console code page.
Not through Console, because charset() only reports the encoding. Change the terminal instead, for example with chcp 65001 in Windows Command Prompt before starting the program.