简体   繁体   English

无法在 Java 中找到或加载 main 类

[英]Could not find or load main a class in Java

i have the following project structure:我有以下项目结构:

JavaTest
    Main
        Main.java
    Test
        Test.java

Main.java:主.java:

package Main;
import Test.*;
public class Main {
    public static void main(String[] args) {
    }
}

Test.java:测试.java:

package Test;
public class Test {
} 

I compile them with the following commands:我使用以下命令编译它们:

D:\Development\Workspace\JavaTest>javac Main\Main.java

D:\Development\Workspace\JavaTest>javac Test\Test.java

The class files are put like this:类文件是这样放置的:

JavaTest
    Main
        Main.java
        Main.class
    Test
        Test.java
        Test.class

I'm trying to run it with the following command:我正在尝试使用以下命令运行它:

D:\Development\Workspace\JavaTest>java -cp D:\Development\Workspace\JavaTest\Main;D:\Development\Workspace\JavaTest\Test Main

The error i'm getting is:我得到的错误是:

Error: Could not find or load main class Main
Caused by: java.lang.NoClassDefFoundError: Main/Main (wrong name: Main)

add the package to your main class and set the classpath to the base directory将包添加到您的主类并将类路径设置为基目录

D:\Development\Workspace\JavaTest>java -cp D:\Development\Workspace\JavaTest Main.Main

I'd recommend you stick to Java conventions and use only lowercase names in your package.我建议您坚持 Java 约定并在您的包中仅使用小写名称。 And you don't need the Test path to run your class而且您不需要测试路径来运行您的课程

Do it as follows:请按以下步骤操作:

javac -d . Main/Main.java
java Main.Main

Notes:笔记:

  1. The current directory is represented by a .当前目录由.
  2. I recommend you follow the Java naming convention.我建议您遵循 Java 命名约定。 As per the naming convention, the name of your package should be main .根据命名约定,您的包的名称应该是main Check https://www.oracle.com/technetwork/java/codeconventions-135099.html for more details.查看https://www.oracle.com/technetwork/java/codeconventions-135099.html了解更多详情。
  3. Use the command, javac -help to know more about the options available with javac .使用命令javac -help了解有关javac可用选项的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM