How to get charset of Process in Java?

Viewed 83

Charset.defaultCharset() and file.encoding is JVM's charset, not charset of OS, console and terminal.

Now I create a Process to run a program, and use process.getInputSteam() to read the output, how to find the correct charset for the process (sun.jnu.encoding is right but it seems not general)?

1 Answers

From Java 17 onwards, there is a method in Process class named inputStream.

The source code taken from Process.java:

public final BufferedReader inputReader() {
    return inputReader(CharsetHolder.nativeCharset());
}

The nativeCharset is obtained from System property "native.encoding".

This property was introduced in Java 17. This is probably what you want.

Reference: https://bugs.openjdk.java.net/browse/JDK-8266075

Related