Java could not find or load a solution to the main class and encoding error problem
- 2021-06-28 12:48:04
- OfStack
Give the code first (the current directory is: D:\pro):
package org.test;
public class TestJava{
public static void main(String args[]){
System.out.println("Hello World!!!");
System.out.println(" Hello, Java!!");
}
}
1. When the cmd window is running, there is a problem that the main class cannot be found or loaded:
D:\pro>javac TestJava.java
D:\pro>java TestJava
error : The main class could not be found or loaded TestJava
The main reason for this problem is the presence of package packages in the program.So there are two ways to handle packages.
(1) Manually create a new package directory:
If new under current directory: (d:pro) > org > test
Then move the compiled TestJava.class file to the d:proorgtest directory.
Run java org.test.TestJava at this time
Now you can output the correct results.
D:\pro > javac TestJava.java
D:\pro > java org.test.TestJava
Hello World!!!
Hello, Java!!
(2) Automatically create package directories:
Run Code:
javac -d . TestJava.java
java org.test.TestJava
(Note: "-d." "There is a space before this point, otherwise it is still a problem to run.)The correct results can also be output:
D:\pro > javac -d . TestJava.java
D:\pro > java org.test.TestJava
Hello World!!!
Hello, Java!!
2. The cmd window runs with garbled code:
Since most of our systems are Chinese, and many of our programming software are compiled in English, when running code containing Chinese, it is easy to encoding problems, at this time the code needs to be changed.
D:\pro>javac -d . TestJava.java
TestJava.java:4: error : Code GBK Unmappable Characters
* @date Time: 2016 Squid ?4 � ?22 � ヤ � � ?3:07:49
^
TestJava.java:4: error : Code GBK Unmappable Characters
* @date Time: 2016 Squid ?4 � ?22 � ヤ � � ?3:07:49
^
TestJava.java:4: error : Code GBK Unmappable Characters
* @date Time: 2016 Squid ?4 � ?22 � ヤ � � ?3:07:49
^
3 Errors
Therefore, it is necessary to change the Chinese gbk code to utf-8 code at this time:
Code: java-encoding UTF-8 TestJava.java
D:\pro>javac -encoding UTF-8 -d . TestJava.java
D:\pro>java org.test.TestJava
Hello World!!!
Hello, Java !!
As you can see from the code above, when the program has both packages and incorrect encoding, we can also solve it once.
summary