Home >Java >JavaBase >Java error: Reasons and solutions for main class not found or unable to be loaded

Java error: Reasons and solutions for main class not found or unable to be loaded

尚
Original
2019-11-28 09:45:0164055browse

Java error: Reasons and solutions for main class not found or unable to be loaded

1. Problem location

When compiling (javac) and executing (java) java program, this type of error occurs: cannot be found or cannot load the main class: (Recommended: java video tutorial)

1. First, rule out whether the problem is caused by improper configuration of environment variables. Just make sure that the command line interface can recognize javac/ java command, it means that there is no problem with the environment variable configuration.

2. This problem often occurs because the java source file contains a package name, such as the file C:\code\Hello.java:

package com.example;

public class Hello{
    public static void main(String[]args){
        System.out.println("Hello");
    }
}

It seems that there is no problem, Execution:

C:\code>javac Hello.java
C:\code>java Hello
错误: 找不到或无法加载主类 Hello

2. Solution

Delete the package name from the source file (not recommended);

Create it under code with the same name as the package The file path structure (C:\code\com\example\Hello.java)

编译:C:\code>javac com/example/Hello.java
运行:C:\code>java com.example.Hello

For more java knowledge, please pay attention to the java Basic Tutorial column.

The above is the detailed content of Java error: Reasons and solutions for main class not found or unable to be loaded. 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