-1

I've been trying to set the default java encoding character set from "windows-1252" to "UTF8" and its not working.

I've tried using java -Dfile.encoding=UTF8 and I get

Error: Could not find or load main class .encoding=UTF8
Caused by: java.lang.ClassNotFoundException: /encoding=UTF8

in the VS code terminal and that doesn't work. I've also tried set JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8 and that doesn't work either I literally get nothing and the default encoding isn't changed. The curious thing is I can do this on the windows command prompt and when entering java -version I am told the encoding is set to UTF8. However using Charset.defaultCharset() at the beginning of the program is telling me my encoding is set to "windows-1252".

  • For the JVM startup parameter are you actually providing the class / jar to be executed? See https://stackoverflow.com/a/362006/664577 for more information. – Anthony Accioly Apr 24 '23 at 12:58
  • In doing this would it change the java default encoding char set for all classes that I run? the intention is to permanently change it so when I run a class or program I don't have to worry about encoding problems – 404-No Social Skills Found Apr 24 '23 at 18:50
  • If (and only if) you are on Windows and want to globally and permanently change the default charset for your machine to UTF-8, then update your _locale_ information as follows: **{Control Panel}** > **Region** > select the **Administrative** tab > Click the **Change System Locale...** button > Check the checkbox labeled _"Beta: Use Unicode UTF-8..."_, and restart your machine. Then you won't need to bother about setting `file.encoding`. – skomisa Apr 24 '23 at 22:46
  • 1
    The value returned by method `Charset defaultCharset()` depends on the JDK being used. For JDK 17 it _"[typically depends upon the locale and charset of the underlying operating system](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/charset/Charset.html#defaultCharset())"_, but for JDK18+ the _"[default charset is UTF-8, unless changed in an implementation specific manner](https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/nio/charset/Charset.html#defaultCharset())"_. So update your question to state which JDK you are using - that's essential information. – skomisa Apr 25 '23 at 05:51

1 Answers1

1

You will see a similar problem from Windows Powershell, either switch to CMD.EXE or try putting the parameter in quotes, and remember to add the rest of the command line (runnable jar or class to launch):

java "-Dfile.encoding=UTF8" ... rest of command line with classname

Without the quotes, Powershell is passing in the arguments as if you typed following which defines a System property "file" with no value and classname ".encoding=UTF8":

java -Dfile .encoding=UTF8

Another way to switch over to default UTF-8 file encoding is by upgrading to JDK18 or above, it does not require any command line switch. See JEP 400: UTF-8 by Default, but you ought to test your applications with -Dfile.encoding=UTF8 beforehand.

DuncG
  • 12,137
  • 2
  • 21
  • 33
  • I've tried that and it results in java command usage documentation but doesn't actually change the default encoding. – 404-No Social Skills Found Apr 24 '23 at 18:27
  • The default only changes for that launch, you obviously need to add the remaining arguments including classname to launch your application and so avoid seeing the command usage documentation. – DuncG Apr 25 '23 at 07:22