Default to a locale in Java stub template
On non-macOS Unix, without any locale variable set, the OpenJDK defaults
to using ASCII rather than as the encoding for file system paths
(i.e., the value of the `sun.jnu.encoding` property).
Fixes
Closes
@@ -94,6 +94,16 @@ function is_macos() {
[[ "${OSTYPE}" =~ darwin* ]]
}
+function available_utf8_locale() {
+ # Both C.UTF-8 and en_US.UTF-8 do not cause any language-specific effects
+ # when set as LC_CTYPE, but neither is certain to exist on all systems.
+ if [[ $(LC_CTYPE=C.UTF-8 locale charmap 2>/dev/null) == "UTF-8" ]]; then
+ echo "C.UTF-8"
+ elif [[ $(LC_CTYPE=en_US.UTF-8 locale charmap 2>/dev/null) == "UTF-8" ]]; then
+ echo "en_US.UTF-8"
+ fi
+}
+
# Parse arguments sequentially until the first unrecognized arg is encountered.
# Scan the remaining args for --wrapper_script_flag=X options and process them.
ARGS=()
@@ -362,6 +372,17 @@ if [ -z "$CLASSPATH_LIMIT" ]; then
is_windows && CLASSPATH_LIMIT=7000 || CLASSPATH_LIMIT=120000
fi
+# On non-macOS Unix, without any locale variable set, the JVM would use
+# using ASCII rather than UTF-8 as the encoding for file system paths.
+if ! is_macos; then
+ if [ -z ${LC_CTYPE+x} ] && [ -z ${LC_ALL+x} ] && [ -z ${LANG+x} ]; then
+ UTF8_LOCALE=$(available_utf8_locale)
+ if [[ -n "$UTF8_LOCALE" ]]; then
+ export LC_CTYPE="$UTF8_LOCALE"
+ fi
+ fi
+fi
+
if (("${#CLASSPATH}" > ${CLASSPATH_LIMIT})); then
export JACOCO_IS_JAR_WRAPPED=1
create_and_run_classpath_jar
