Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What if there is garbled code in Java Cmd running Jar?

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the Java Cmd running Jar garbled how to do, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.

Garbled code occurred when Java Cmd was running Jar.

There is a headache when working on a project-no matter how it is uniformly encoded to UTF8 in Java code, it is no problem to run in IDE, but when packaged into Jar and run through Bash and CMD, there will be Chinese garbled problems.

At first, I thought there was a problem with the Java compilation code, but after hours of troubleshooting, I finally found the root cause of the Chinese garbled problem-the default code for cmd/bash is GBK. In order to prevent trampling in the future, this paper records the solution to the problem.

1. Set the coding of cmd to utf-8

Open cmd and enter the following command

Chcp 65001

This allows you to change the encoding of cmd to UTF-8.

Here are some common cmd encodings

Character encoding corresponding character

GBK (default) 936

American English 437

Utf-8 65001

2. Stipulate the coding method when executing java

Under cmd, the common commands to run the Jar package are as follows

Java-jar project.jar

But this may cause errors caused by incorrect java runtime coding. At this time, we can specify the encoding parameters when running Jar.

Java-Dfile.encoding=utf-8-jar project.jar

Third, package the above commands into bat

If you have to do the above two steps every time you start the project, it will be very troublesome. We can package the above two parts into a bat batch file, as follows

3.1 create a txt file in the Jar directory

Open it through notepad

3.2 write the above commands to the txt file and save

3.3 change msgSystemStart.txt to msgSystemStart.bat

3.4 run the bat file

As you can see, the project starts normally and there is no garbled problem.

Through the above steps, the problem of Chinese garbled can basically be solved.

Cmd runs the java file. 1. Go to the directory where the java file is located

Suppose our A.java file is in the F:\ data structure\ course design\ 45 directory.

Open cmd and enter F disk.

Then enter "cd\ data structure\ course Design\ 45" to enter the file storage path. After success, you will see that the command line prefix is with a path.

II. Compiling java files

After successfully entering, type "javac A.java", and there is no response in the cmd interface, but a class file of A.class will be automatically generated in the directory, indicating that the compilation is successful.

Then go back to the command line and enter "java A", enter the test data, and get the result.

Third, import txt file for testing

The in.txt file is the input data and out.txt is the output data.

Note that the txt file is best placed in the same directory as the java file

Then the out.txt will be generated under the 45 file.

Thank you for reading this article carefully. I hope the article "what to do when Java Cmd runs Jar is garbled" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report