Home >Java >javaTutorial >How to solve Java 'Unable to find or load main class' error message

How to solve Java 'Unable to find or load main class' error message

王林
王林forward
2023-04-24 16:37:087584browse

Preface

Generally, we use tools to edit and debug code, such as eclipse, Manven, Android Studio, sublime, vim, notepad, Notepad, etc.
When we create project and java class files using eclipse android studio, etc., they all have package names. They are compiled and run normally with tools, but when we switch to the command line for execution:
javac xxx.java
java xxx
It is very likely that cannot find or cannot load the main class

When we first learn Java, the class file is If the package name is not set, pay attention to the classpath in this case, and there is basically no problem. But when we use tools (eclipse, android studio) to write code, the location of the code file is in the directory separated by the package name. Note that this is a relative physical directory (com/eagle/app). And the package name (package) is declared in the code, which can be considered as a virtual directory (com.eagle.app). This article mainly explains the cause of the error and the correct solution when there is no problem with the classpath and the system environment variable PATH.
The code file is very simple as follows:

package com.eagle.app;

public class MainJava {
	public static void main(String[] args) {
        if (args != null) {
            for (String arg : args) {
                System.out.println("arg = " + arg);
            }
            System.out.println("arg = " + args);
        } else {
            System.out.println("args = " + null);
        }
}

javac xxx.java Compilation requires a relative physical path

How to solve Java Unable to find or load main class error message

As shown above, taking Android studio as an example
1. There is a src directory in the project directory, but it contains the files of the entire app, not the "src" directory of the code; in the case of eclipse, the src directory is the code directory.
2. This is the "src" directory of the code, and there is a "directory" corresponding to our package name one-to-one. com/eagle/app

To compile MainJava, use:

//cd 到app目录
javac MainJava.java

or

//cd  到eagle 目录
javac  app/MainJava.java

or even

//cd 到main目录
javac  java/com/eagle/app/MainJava.java

are okay. Of course, it can also be a relative directory at any location.

Let’s go back and explain: relative to the currently executed directory (such as app directory, eagle directory, main directory), the physical path is a valid computer path (xxx/xxx/xxx), so the name is relative to the physical path.

If something goes wrong, the path must be wrong.
Note: The code directory is determined based on the starting directory of the package name when creating a new code file

java xxx execution requires a virtual path

This is a bit interesting, let me emphasize,Package name virtual path: xxx.xxx.xxx, it is not a directory separated by "/", so it is called a virtual path.

How to solve Java Unable to find or load main class error message

In the above picture, 2 is the java code, 1 is the bytecode file generated by tool compilation, now we need to execute MainJava.class, the correct command is:
/ /cd to the main directory in 1, not to com or a lower directory

F:\GSProject>cd javatest\build\classes\java\main

F:\GSProject\javatest\build\classes\java\main>java com.eagle.app.MainJava

Note: Do not bring .class (java com.eagle.app.MainJava.class is incorrect), and do not try Use a relative directory to change to other directories, because the java command recognizes the following parameters as the package name.
The following are all wrong

F:\GSProject\javatest\build\classes\java\main>java com.eagle.app.MainJava.class
Error: not found Or the main class com.eagle.app.MainJava.class

F:\GSProject\javatest\build\classes\java>java main\com.eagle.app.MainJava# cannot be loaded. ##Error: The main class main\com.eagle.app.MainJava cannot be found or cannot be loaded

Okay, now we will move the directory circled by 1 in the above picture to the javatest directory. We execute java com.eagle.app.MainJava

How to solve Java Unable to find or load main class error message

##

F:\GSProject\javatest>java com.eagle.app.MainJava
arg = [Ljava.lang.String;@75b84c92
How to solve Java Unable to find or load main class error message in the javatest directory. The result is that the parameter address of the main function is output normally. It can be seen that we can run our bytecode at will, put it on Linux or Mac, and happily do what we want to do. The directory structure in

1 is package com.eagle.app; be sure to make sure that the javac file path is **/xxx/xxx/xxx/Name.java**, and the

java file path is

xxx.xx.x.Name
, do not bring class.

The above is the detailed content of How to solve Java 'Unable to find or load main class' error message. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete