3

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)?

FredSuvn
  • 1,869
  • 2
  • 12
  • 19
  • Check if this helps: https://stackoverflow.com/questions/1066845/what-exactly-is-sun-jnu-encoding – pringi Mar 10 '22 at 14:15

1 Answers1

2

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

pringi
  • 3,987
  • 5
  • 35
  • 45
  • 1
    Be aware this may change in Java 18, when most character-encoding defaults will change to UTF-8 across all platforms. See [*JEP 400: UTF-8 by Default*](http://openjdk.java.net/jeps/400). – Basil Bourque Mar 10 '22 at 15:35