0
PS D:\> cd "D:\IdeaProjects\demos"
PS D:\IdeaProjects\demos> ls


    目录: D:\IdeaProjects\demos


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2024/9/29     13:24                .idea
d-----         2024/9/29     13:21                src
d-----         2024/9/29     13:24                target
-a----         2024/9/29     13:12           2461 .gitignore
-a----         2024/9/29     13:23           2591 pom.xml
-a----         2024/9/29     13:25              0 Simplified Chinese 简体中文.txt


PS D:\IdeaProjects\demos> java -jar D:\IdeaProjects\demos\target\grabled-chars-1.0-SNAPSHOT-jar-with-dependencies.jar
SLF4J(I): Connected with provider of type [ch.qos.logback.classic.spi.LogbackServiceProvider]
13:25:49.259 [main] INFO Application -- Simplified Chinese 绠€浣撲腑鏂?

PS D:\IdeaProjects\demos>

From the result of ls ,we can see Simplified Chinese 简体中文.txt is displayed well.

And here is the source code of grabled-chars-1.0-SNAPSHOT-jar-with-dependencies.jar. It is very simple,

@Slf4j
public class Application {
    public static void main(String[] args) throws InterruptedException {
        log.info("Simplified Chinese 简体中文");
    }
}

with Lombok and logback-classic:1.5.8.

But the output in PowerShell is 13:25:49.259 [main] INFO Application -- Simplified Chinese 绠€浣撲腑鏂?

How can I fix this?

6
  • 2
    Try: $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding, see: Using UTF-8 Encoding (CHCP 65001) in Command Prompt / Windows Powershell (Windows 10) Commented Sep 29, 2024 at 10:13
  • (1) Power Shell run command chcp , show current codepage (2) 3 possible setting , 936(GBK) , 437 , 65001 (UTF-8) (3) PS C:\Users\User\demos-garbled-chars\target> chcp 活动代码页: 936 PS C:\Users\User\demos-garbled-chars\target> java -jar grabled-chars-1.0-SNAPSHOT-jar-with-dependencies.jar SLF4J(I): Connected with provider of type [ch.qos.logback.classic.spi.LogbackServiceProvider] 04:38:54.128 [main] INFO Application -- Simplified Chinese 简体中文 Commented Sep 29, 2024 at 13:14
  • (4) PS C:\Users\User\demos-garbled-chars\target> chcp Active code page: 437 PS C:\Users\User\demos-garbled-chars\target> java -jar grabled-chars-1.0-SNAPSHOT-jar-with-dependencies.jar SLF4J(I): Connected with provider of type [ch.qos.logback.classic.spi.LogbackServiceProvider] 05:00:32.281 [main] INFO Application -- Simplified Chinese ???? (But in en_US, code page 437, Power Shell shows ?????? , which I think is correct.) Commented Sep 29, 2024 at 13:15
  • 1
    Hi @iRon Many thanks for your help, after executing $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding in powershell, now output of jar -jar is displayed well! Commented Sep 29, 2024 at 13:39
  • Hi @life888888 Many thanks your advice, but we followed advice from iRon first and it works, so I cannot verify if your solution can help. And would you mind wrting it as a formal answer? currently it is a bit unfriendly to read. Commented Sep 29, 2024 at 13:40

1 Answer 1

1

Base on your source code (https://github.com/XSun771/demos/tree/garbled-chars)

  • Windows codepage 936 (GBK) + JDK 21 will get the same error message (Simplified Chinese 绠€浣撲腑鏂?).
  • Windows codepage 936 (GBK) + JDK 17 will get the correct message.

Solution 1 - Remove JDK 21 and install JDK 17

Solution 2 - Add logback.xml config file

Project Tree

demos-garbled-chars
├── pom.xml
├── src
│   └── main
│       ├── java
│       │   └── Application.java
│       └── resources
│           └── logback.xml
...

Add logback.xml to your project

  • ADD <charset>GBK</charset> to ConsoleAppender - encoder
  • ADD <charset>UTF-8</charset> to RollingFileAppender - encoder
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d [%thread] %-5level %-50logger{40} - %msg%n</pattern>
            <charset>GBK</charset>
        </encoder>
    </appender>
     
    <appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>app.log</file>
        <encoder>
            <pattern>%d [%thread] %-5level %-50logger{40} - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
         
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>app-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxFileSize>1MB</maxFileSize>
            <maxHistory>30</maxHistory>
            <totalSizeCap>10MB</totalSizeCap>
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>
    </appender>
     
    <root level="INFO">
        <appender-ref ref="Console" />
        <appender-ref ref="RollingFile" />
    </root>
</configuration>

build & run

build

mvn clean package

run

java -jar grabled-chars-1.0-SNAPSHOT-jar-with-dependencies.jar

Powershell Will show Correct output message.

and log file app.log use UTF-8 , log the correct output message.

Solution 3: Use JVM parameters to adjust encoding. (Not tested)

You can choose Solution 1 or Solution 2 as your solution.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.