Skip to content

Commit

Permalink
Don't use "file.encoding", use Platform.getSystemCharset() instead
Browse files Browse the repository at this point in the history
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.getSystemCharset() 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
eclipse-platform/eclipse.platform.resources#154
  • Loading branch information
iloveeclipse committed Jun 22, 2022
1 parent c07ab53 commit acf3354
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,7 +38,6 @@ public class WorkbenchEncoding {
private static class EncodingsRegistryReader extends RegistryReader {

private List<String> encodings;

/**
* Create a new instance of the receiver.
*
Expand All @@ -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<String> 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.getSystemCharset().name();
}
defaultEncoding = encoding;
}
return defaultEncoding;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down

0 comments on commit acf3354

Please sign in to comment.