Question:
No matter how the Java code is uniformly encoded as UTF8, it is completely fine to run in the IDE, but it is packaged into Jar and run through Bash and CMD. , Chinese garbled characters will appear.
Reason:
The default encoding of cmd/bash is GBK.
Solution:
1. Set the cmd encoding to utf-8
Open cmd and enter the following command:
chcp 65001
This way You can change the encoding of cmd to UTF-8.
The following is the commonly used cmd encoding, as shown in the figure:
2. Specify the encoding method when executing java
Under cmd, The common commands to run Jar packages are as follows:
java -jar project.jar
However, this may cause errors caused by incorrect encoding during java runtime. At this time, we can specify the encoding parameters when running Jar
java -Dfile.encoding=utf-8 -jar project.jar
Three . Package the above commands into a bat
If you have to perform the above two steps every time you start the project, it will actually be very troublesome. We can package the above two steps into a bat batch file. The specific steps are as follows
3.1 Create a txt file in the Jar directory and open it with Notepad
3.2 Write the above command into the txt file and save it
3.3 Modify msgSystemStart.txt to msgSystemStart.bat
##3.4 Run the bat file You can see that the project starts normally and there will be no garbled code problems. Recommended tutorial:The above is the detailed content of Garbled characters appear when running java projects through bash/cmd. For more information, please follow other related articles on the PHP Chinese website!