Error: Could not find or load main class Test4 solution

Under Windows, you can use the javac command to compile java files, but the main class cannot be found or cannot be loaded, as shown in the figure below.

When this result occurs, first check whether your java environment variables are configured correctly. The environment configuration link is as follows:

java environment variable configuration ( https://blog.csdn.net/stay_zezo/article/details/80155888 )



If the above java environment variables are correct, the java program can be compiled but cannot be executed. It should be because the package name in the java file causes the execution program to fail.

For example the following java file:

package test;
public class Test4 {
public static void main(String[] args) {
System.out.println("测试");
}
}

solution:

If you don't reference other java files in the package, use option 1.

If other java files in the package are referenced, use scheme 2, otherwise the program cannot find other files in the java package.

Option 1. Open the java file, delete the  package test; and  save it, and recompile the java file with javac. Then it can be executed.

Option 2. When compiling java, use the following format to compile: javac java filename.java -d .   

Note that there is a space between -d and the dot

For example the above program: javac Test4.java -d .


When executing a java program, you need to add the package name. Format: java package name.java file name

E.g:

java test.Test4


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325605757&siteId=291194637