Home  >  Article  >  Java  >  Garbled characters appear when running java projects through bash/cmd

Garbled characters appear when running java projects through bash/cmd

王林
王林Original
2019-11-23 13:56:303436browse

Garbled characters appear when running java projects through bash/cmd

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:

Garbled characters appear when running java projects through bash/cmd

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

Garbled characters appear when running java projects through bash/cmd

3.2 Write the above command into the txt file and save it

Garbled characters appear when running java projects through bash/cmd

3.3 Modify msgSystemStart.txt to msgSystemStart.bat

Garbled characters appear when running java projects through bash/cmd

##3.4 Run the bat file

Garbled characters appear when running java projects through bash/cmd

You can see that the project starts normally and there will be no garbled code problems.

Recommended tutorial:

java quick start

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn