0

I'm trying to launch Spring Boot uber jar with a following command:

java -jar api-0.0.1.jar -Dfile.encoding=UTF-8

but it fails with a following error:

07:52:54.573 [main] INFO com.example.domain.api.Application - Starting application...
2017-08-01 07:52:55.071 ERROR 5792 --- [           main] o.s.b.c.FileEncodingApplicationListener  : System property 'file.encoding' is currently '
Cp1251'. It should be 'UTF-8' (as defined in 'spring.mandatoryFileEncoding').
2017-08-01 07:52:55.074 ERROR 5792 --- [           main] o.s.b.c.FileEncodingApplicationListener  : Environment variable LANG is 'null'. You could
 use a locale setting that matches encoding='UTF-8'.
2017-08-01 07:52:55.075 ERROR 5792 --- [           main] o.s.b.c.FileEncodingApplicationListener  : Environment variable LC_ALL is 'null'. You cou
ld use a locale setting that matches encoding='UTF-8'.
2017-08-01 07:52:55.082 ERROR 5792 --- [           main] o.s.boot.SpringApplication               : Application startup failed

java.lang.IllegalStateException: The Java Virtual Machine has not been configured to use the desired default character encoding (UTF-8).
        at org.springframework.boot.context.FileEncodingApplicationListener.onApplicationEvent(FileEncodingApplicationListener.java:74) ~[spring-b
oot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
        at org.springframework.boot.context.FileEncodingApplicationListener.onApplicationEvent(FileEncodingApplicationListener.java:46) ~[spring-b
oot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) ~[spring
-context-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring
-context-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122) ~[spring
-context-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
        at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74) ~[spring-boot
-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
        at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) ~[spring-boot-1.5.6.R
ELEASE.jar!/:1.5.6.RELEASE]
        at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEAS
E]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:296) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
        at com.example.domain.api.Application.main(Application.java:36) [classes!/:0.0.1]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [api-0.0.1.jar:0.0.1]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [api-0.0.1.jar:0.0.1]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [api-0.0.1.jar:0.0.1]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [api-0.0.1.jar:0.0.1]

Also, I have the following properties in my Maven pom.xml:

<properties>
    <java.source.version>1.8</java.source.version>
    <java.target.version>1.8</java.target.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <file.encoding>UTF-8</file.encoding>
</properties>

What am I doing wrong and how to properly launch this application ?

alexanoid
  • 24,051
  • 54
  • 210
  • 410

2 Answers2

3

two encoding sets supported by Java SE 6. The canonical names used by the new java.nio APIs are in many cases not the same as those used in the java.io and java.lang APIs.

  • if you are use Canonical Name for java.nio API, you need to use : UTF-8
  • if you are use Canonical Name for java.io and java.lang API, you need to use : UTF8

so use -Dfile.encoding=UTF-8 or -Dfile.encoding=UTF8

Mayank Sharma
  • 403
  • 2
  • 2
  • I tried all of these variants: `java -jar api-0.0.1.jar` `java -jar api-0.0.1.jar -Dfile.encoding=UTF-8` `java -jar api-0.0.1.jar -Dfile.encoding=UTF8` Unfortunately no one works – alexanoid Aug 01 '17 at 07:28
  • 3
    I found the reason - the order of application arguments does matter, now it works: `java -Dfile.encoding=UTF-8 -jar api-0.0.1.war` – alexanoid Aug 01 '17 at 08:07
  • mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dfile.encoding=UTF-8" works. But i want to configure this in a configuration file – Higarian Jun 02 '20 at 19:25
0

System property 'file.encoding' is currently 'Cp1252'. It should be 'UTF-8' (as defined in 'spring.mandatoryFileEncoding').

Using Netbeans 12.0 on Windows 10 I was able to fix the error by modifying nbactions.xml like this:

Original:

<?xml version="1.0" encoding="UTF-8"?>
<actions>
    <!-- ... -->
    <action>
        <properties>
            <exec.args>-classpath %classpath br.com.example.Application</exec.args>
            <exec.executable>java</exec.executable>
        </properties>
    </action>
    <!-- ... -->
</actions>

Added -Dfile.encoding=UTF-8 in first position of exec.args:

<?xml version="1.0" encoding="UTF-8"?>
<actions>
    <!-- ... -->
    <action>
        <properties>
            <exec.args>-Dfile.encoding=UTF-8 -classpath %classpath br.com.example.Application</exec.args>
            <exec.executable>java</exec.executable>
        </properties>
    </action>
    <!-- ... -->
</actions>
WilliamK
  • 1,633
  • 15
  • 12