Record the process of solving the console garbled characters when the jar package is running and repackaging the modified content of the jar package

1. Springboot is packaged as an executable jar package

1. Add the following code to the pom.xml file:

<packaging>jar</packaging>

2. Add the maven plugin to the pom.xml file:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

3. Click the maven menu on the right side of IDEA, double-click the clean command
insert image description hereinsert image description here
4. Double-click the package command
insert image description here
to targetgenerate an executable jar file in the directory

5. Execute
the use java -jar **.jarcommand in the cmd window to run the jar file just generated
insert image description here

2. The console solves Chinese garbled characters

What I use here is log4j2to implement the log.
Since the encoding of the cmd control window is GBKyes, if we configure the console encoding method as the log, UTF-8garbled characters will appear.
Therefore, in the log4j2 configuration file, change the encoding of the console to GBK, and the encoding in the file output is still the same UTF-8, which can ensure that the console and log files can display Chinese normally.

<!--输出控制台的配置 -->
<console name="Console" target="SYSTEM_OUT">
    <!--输出日志的格式 -->
    <PatternLayout charset="GBK" pattern="[%-5p] %d{yyyy-MM-dd HH:mm:ss} %l%n%m%n" />
</console>

The encoding of the log file is set to UTF-8

<PatternLayout charset="UTF-8" pattern="[%-5p] %d{yyyy-MM-dd HH:mm:ss} method:%l%n%m%n%n" />

3. Modify the content of the jar package and repackage it

Sometimes after we deploy to the server, we need to modify some configurations of the configuration file. At this time, it is necessary to unzip the file first jar, modify the content and then repackage it into jara file.
1. Decompress the jar file
(1) You can modify the suffix name zip, and then decompress
(2) You can use winRAR to directly right-click to decompress
2. Pack the jar file
After modification, enter the decompressed directory, and enter the following command in cmd:

jar cvfM0 my-jar.jar *

It can be packaged back to the executable file, and the modified configuration file takes effect.

Guess you like

Origin blog.csdn.net/qq_38118138/article/details/122324012