From d491f0d07284891d08d8f3c686ff6b76712f579e Mon Sep 17 00:00:00 2001 From: Andrey Loskutov Date: Fri, 10 Jun 2022 13:51:27 +0200 Subject: [PATCH] Don't use "file.encoding", use Platform.getNativeEncoding() instead With https://openjdk.java.net/jeps/400 implemented in Java 18, "file.encoding" system property became meaningless and can't be used anymore to determine system native encoding. Instead, use new Platform.getNativeEncoding() API that tries to provide a suitable replacement compatible with Java 18+ and previous Java versions. Note: the proposed change makes sure that IF users have specified encoding via command line arguments -Dfile.encoding=XYZ, it will be still used. This PR fixes https://github.com/eclipse-platform/eclipse.platform.resources/issues/154 --- .../org/eclipse/ui/WorkbenchEncoding.java | 23 +++++++++++++++++-- .../META-INF/MANIFEST.MF | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/WorkbenchEncoding.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/WorkbenchEncoding.java index d1e38f0c9df..8706d2b5951 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/WorkbenchEncoding.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/WorkbenchEncoding.java @@ -13,6 +13,7 @@ *******************************************************************************/ package org.eclipse.ui; +import java.lang.management.ManagementFactory; import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; import java.util.ArrayList; @@ -37,7 +38,6 @@ public class WorkbenchEncoding { private static class EncodingsRegistryReader extends RegistryReader { private List encodings; - /** * Create a new instance of the receiver. * @@ -58,13 +58,32 @@ protected boolean readElement(IConfigurationElement element) { } } + private static String defaultEncoding; + /** * Get the default encoding from the virtual machine. * * @return String */ public static String getWorkbenchDefaultEncoding() { - return System.getProperty("file.encoding", "UTF-8");//$NON-NLS-1$ //$NON-NLS-2$ + if (defaultEncoding == null) { + String encoding = null; + // Check if -Dfile.encoding= startup argument was given to Eclipse's JVM + List commandLineArgs = ManagementFactory.getRuntimeMXBean().getInputArguments(); + for (String arg : commandLineArgs) { + if (arg.startsWith("-Dfile.encoding=")) { //$NON-NLS-1$ + encoding = arg.substring("-Dfile.encoding=".length()); //$NON-NLS-1$ + // continue, it can be specified multiple times, last one wins. + } + } + // Now we are sure user didn't specified encoding, so we can use + // default native encoding + if (encoding == null || encoding.isBlank()) { + encoding = Platform.getNativeEncoding().name(); + } + defaultEncoding = encoding; + } + return defaultEncoding; } /** diff --git a/bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF b/bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF index f71388fa856..c1ad38cfca4 100644 --- a/bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF @@ -95,7 +95,7 @@ Export-Package: org.eclipse.e4.ui.workbench.addons.perspectiveswitcher;x-interna org.eclipse.ui.themes, org.eclipse.ui.views, org.eclipse.ui.wizards -Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.25.0,4.0.0)", +Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.26.0,4.0.0)", org.eclipse.help;bundle-version="[3.2.0,4.0.0)", org.eclipse.jface;bundle-version="[3.18.0,4.0.0)", org.eclipse.swt;bundle-version="[3.107.0,4.0.0)",