1. Write the code to the notebook, naming it the same as the class name, with a suffix of .java
.
example:H.java
class H{
public static void main(String[] args){
System.out.println("Hello world");
}
}
2. "Window+R", enter "cmd", click 'OK'; enter the folder where the note is located, E:\Desktop>
enter the command and press javac H.java
Enter (to compile), enter the command java H
to run after the compilation is completed.
3. Chinese garbled code solution
class H{
public static void main(String[] args){
System.out.println("Hello world 你好 世界");
}
}
The following garbled characters appear:
If there are Chinese garbled characters when running
1. 记事本另存文件编码格式为ANSI
2. 如果使用utf-8, java -Dfile.encoding=utf-8 H.java 直接运行解决乱码(java17 可以不用编译直接运行源程序)
3. 如果使用utf-8, javac H.java -encoding utf-8 ,再运行 java H就没有乱码
(1) The encoding format of the file saved in Notepad is ANSI, as shown in the figure below:
(2) Use utf-8, java -Dfile.encoding=utf-8 H.java to run directly to solve the garbled characters (java17 can run the source program directly without compiling) (3)
If you use utf-8, javac H.java -encoding utf-8, and then run java H, there will be no garbled characters