1

System.out.println(Charset.defaultCharset().displayName()); prints ISO-8859-1, so I want to set the defaultCharset to UTF-8.

When tried with java -Dfile.encoding=utf-8 -jar XXX.jar, I checked that default charset is set to UTF-8.

But is there a way to set Dfile.encoding=utf-8 option at maven packaging level, so that I can just run java -jar XXX.jar?

What I tried:

  • mvn clean package -Dfile.encoding=utf-8

  • pom.xml

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <argLine>-Dfile.encoding=utf-8</argLine>
                </configuration>
            </plugin>
  • MAVEN_OPTS="-Dfile.encoding=utf-8" mvn clean package

I prefer controlling with pom.xml file.

yoon
  • 1,177
  • 2
  • 15
  • 28
  • 2
    You shouldn't make wrong assumptions about default character encoding in the first line. It's easier to control your own code than to control external environment. – 9ilsdx 9rvj 0lo Jan 04 '22 at 12:03
  • @9ilsdx9rvj0lo What do you mean by "control your own code"? Referring https://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding, I thought setting default charset cannot be done by code level. If I'm getting anything wrong, please let me know. – yoon Jan 04 '22 at 12:18
  • The answer may depend on your goal - why you want to change it? In tests? Resource filtering? Sources encoding? – Lesiak Jan 04 '22 at 12:32
  • @Lesiak In a log file, a certain language is logged as `???`. I found that it's properly logged when given `Dfile.encoding=utf-8` option, so I want to achieve it without passing Dfile.encoding option everytime I run the app. – yoon Jan 04 '22 at 12:41
  • @yoon Do you write this log file in your own code? Then you might just define the `Charset` argument for your `OutputStreamWriter`/`PrintWriter` constructor. – Alexey Veleshko Jan 04 '22 at 14:23
  • @yoon you should never use default encoding! – 9ilsdx 9rvj 0lo Jan 04 '22 at 16:22

2 Answers2

0

The question is how to pass JVM args for Maven build before runtime.

Starting with Maven 3.3.1+ you can define JVM configuration via ${maven.projectBasedir}/.mvn/jvm.config file which means you can define the options for your build on a per project base. This file will become part of your project and will be checked in along with your project.

So, create .mvn/jvm.config in your project root with content:

-Dfile.encoding=utf-8

See the reference: https://maven.apache.org/configure.html#mvn-jvm-config-file

Max Daroshchanka
  • 2,698
  • 2
  • 10
  • 14
  • I used `maven surefire plugin` without knowing what it exactly is.. Adding your lines to pom.xml didn't solve the problem. `System.out.println(Charset.defaultCharset().displayName());` still prints `ISO-8859-1` – yoon Jan 04 '22 at 12:44
  • @yoon I see, you need to set up JVM for maven before it start. I've updated the answer, please try this. – Max Daroshchanka Jan 04 '22 at 13:25
0

You can define JDK_JAVA_OPTIONS environment variable as -Dfile.encoding=utf-8 to avoid typing it every time. See the java command documentation for details.

In OpenJDK 18 UTF-8 will be default for all operating systems: JEP 400.

Alexey Veleshko
  • 792
  • 1
  • 18
  • Thanks for your answer. But actually I'm finding a way to include that encoding option into a jar or an executable file, so that another person can just run the app without knowing that a certain process(setting env, adding java option, ...) is required. – yoon Jan 04 '22 at 14:10
  • In principle there is `System.setProperty` method, you might call it from `main`. But it's a bad practice. This sort of option is supposed to be set from the command line. – Alexey Veleshko Jan 04 '22 at 14:20