2

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".

7
  • For the JVM startup parameter are you actually providing the class / jar to be executed? See stackoverflow.com/a/362006/664577 for more information. Commented Apr 24, 2023 at 12:58
  • 1
    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 Commented Apr 24, 2023 at 18:50
  • 3
    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. Commented Apr 24, 2023 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", but for JDK18+ the "default charset is UTF-8, unless changed in an implementation specific manner". So update your question to state which JDK you are using - that's essential information. Commented Apr 25, 2023 at 5:51
  • @skomisa your solution is the best, no need further hacking. Commented Jan 28, 2024 at 3:43

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I've tried that and it results in java command usage documentation but doesn't actually change the default encoding.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.