Home  >  Article  >  Java  >  Solution to garbled characters in java when running cmd

Solution to garbled characters in java when running cmd

尚
Original
2019-12-03 11:19:2310485browse

Solution to garbled characters in java when running cmd

The garbled characters when executing the java file in cmd are as follows: (Recommended: java video tutorial)

Solution to garbled characters in java when running cmd

Reason :

Since the JDK is an international version, when compiling, if we do not use the -encoding parameter to specify the encoding format of our JAVA source program, javac.exe will first get the default encoding used by our operating system. encoding format, that is, when compiling a java program, if we do not specify the encoding format of the source program file, JDK first obtains the file.encoding parameter of the operating system (it saves the default encoding format of the operating system, such as WIN2k, its The value is GBK), and then JDK converts our java source program from the file.encoding encoding format to the default UNICODE format within JAVA and puts it into memory.

Then, javac compiles the converted unicode format file into a .class class file. At this time, the .class file is UNICODE encoded, and it is temporarily placed in the memory. Then, JDK converts this file into a UNICODE file. The encoded and compiled class file is saved to our operating system to form the .class file we see.

For us, the .class file we finally obtained is a class file whose content is saved in UNICODE encoding format. It contains the Chinese string in our source program, but at this time it has been passed through file. The encoding format is converted to UNICODE format.

When we compile without setting, it is equivalent to using the parameters: javac -encoding gbk XX.java. Of course, incompatibility will occur.

Solution:

1. Convert to UTF-8 encoding format, but there are still errors

The solution is: you should use the -encoding parameter to specify the encoding method :javac -encoding UTF-8 XX.java

2. Start-->Computer-->Mouse reverse key (right mouse button)-->Properties-->Advanced system settings--> ;Environment variables-->System variables

New. Then enter: "JAVA_TOOL_OPTIONS" variable, the value is "-Dfile.encoding=UTF-8"

Then confirm, confirm, after confirmation. Open a new command line window and enter the javac command again.

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Solution to garbled characters in java when running 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