Property sun.stdout.encoding is undefined in recent Java versions#407
Property sun.stdout.encoding is undefined in recent Java versions#407wfouche wants to merge 8 commits intojython:masterfrom
Conversation
| String output = Py.getCommandResultWindows("chcp"); | ||
| String output = Py.getCommandResultWindows("chcp.com"); | ||
| /* | ||
| * The output will be like "Active code page: 850" or maybe "Aktive Codepage: 1252." or |
There was a problem hiding this comment.
I'm explaining why the only reliable bit of the message is the number. ISTR this one is German.
Ja. So stimmt es. https://www.mikrocontroller.net/topic/561065
There was a problem hiding this comment.
Reverted back to Aktive.
|
As I commented on the original issue, testing console handling is hard because our CI doesn't really exercise it. So it's down to conscientious local testing. |
| encoding = props.getProperty("sun.stdout.encoding"); | ||
| if (encoding != null) { | ||
| // Windows: these versions of Java return "cp65001" for UTF-8 | ||
| if (encoding.equals("cp65001")) { |
There was a problem hiding this comment.
Shouldn't we only bother with this inside the "if it's Windows" clause?
| if (os != null && os.startsWith("Windows")) { | ||
| // Go via the Windows code page built-in command "chcp". | ||
| String output = Py.getCommandResultWindows("chcp"); | ||
| String output = Py.getCommandResultWindows("chcp.com"); |
There was a problem hiding this comment.
When running chcp from Git-Bash on Windows, it cannot find "chcp" and one has to specify the full name which is "chcp.com". I thought Jython might experience the same issue, but it does not. So will revert this change.
There was a problem hiding this comment.
C:\> where chcp
C:\Windows\System32\chcp.com
|
@jeff5 , the following code fragment is unlikely to be executed on Windows given that stdout.encoding and sun.stdout.encoding seems to cover all bases. if (isWindows) {
// Go via the Windows code page built-in command "chcp".
String output = Py.getCommandResultWindows("chcp");
/*
* The output will be like "Active code page: 850" or maybe "Aktive Codepage: 1252." or
* "활성 코드 페이지: 949". Assume the first number with 2 or more digits is the code page.
*/
final Pattern DIGITS_PATTERN = Pattern.compile("[1-9]\\d+");
Matcher matcher = DIGITS_PATTERN.matcher(output);
if (matcher.find()) {
encoding = "cp".concat(output.substring(matcher.start(), matcher.end()));
if (encoding.equals("cp65001")) {
encoding = "utf-8";
}
return encoding;
}
} |
|
@jeff5 , I installed SDKMAN using Git-Bash on Windows, and this makes it a lot easier to do Jython console testing using different versions of Java. With SDKMAN I can easily install and switch between diffrent JDK versions. First install scoop - https://scoop.sh/ Then run these commands to install Git/Bash, curl, zip and unzip
Lastly, install SDKMAN
Now one can start bash.exe from inside the Jython project folder cd IdeaProjects\jython Bash is just a different Windows shell, so when Jython runs it still runs as it normally would as a Windows process. Don't you think this would make a good addition to the README.md file? |
|
Results of tests performanced on Linux, MacOS and Windows. We can see that for Java 21 and higher, property stdout.encoding is always defined. Older versions of Java don't define property stdout.encoding, and may or may not defined property sun.stdout.encoding. Linux
MacOS
Windows
test.cmd test.sh test.kt val javaVersion = System.getProperty("java.version") ?: "null"
val sun_stdout_encoding = System.getProperty("sun.stdout.encoding") ?: "null"
val stdout_encoding = System.getProperty("stdout.encoding") ?: "null"
fun main() {
print("java.version = " + javaVersion)
print(", sun.stdout.encoding = " + sun_stdout_encoding)
println(", stdout.encoding = " + stdout_encoding)
} |
|
@jeff5 , testing has been completed. Please review once more. Could we eventually release this as Jython 2.7.5? |
Tested with script
on Java 8 and 25.
Fixes #404