简体   繁体   English

使用 package 时出现 java.lang.ClassNotFoundException

[英]Getting java.lang.ClassNotFoundException when using package

I have a java file ComPac.java with the below code:我有一个 java 文件ComPac.java ,代码如下:

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

The file is located at the path: /home/ec2-user/java_c该文件位于路径: /home/ec2-user/java_c

To compile this file, I ran javac Compac.java , and the class file was generated.为了编译这个文件,我运行了javac Compac.java ,并生成了 class 文件。

在此处输入图像描述

Now its turn to run the class file.现在轮到运行 class 文件了。

So I did java ComPac (screenshot below)所以我做了java ComPac (截图如下)

java ComPac

Understandably, I got the error Error: Could not find or load main class ComPac. Caused by: java.lang.NoClassDefFoundError: com/ComPac (wrong name: ComPac).可以理解的是,我收到错误Error: Could not find or load main class ComPac. Caused by: java.lang.NoClassDefFoundError: com/ComPac (wrong name: ComPac). Error: Could not find or load main class ComPac. Caused by: java.lang.NoClassDefFoundError: com/ComPac (wrong name: ComPac). This I am assuming is because the java file has the package com declared in it.我假设这是因为 java 文件中声明了 package com

So instead I tried, java com.ComPac and expected it to work(screenshot below).所以我尝试java com.ComPac并期望它可以工作(截图如下)。

java com.ComPac

But I got the error: Error: Could not find or load main class com.ComPac. Caused by: java.lang.ClassNotFoundException: com.ComPac但我收到错误: Error: Could not find or load main class com.ComPac. Caused by: java.lang.ClassNotFoundException: com.ComPac Error: Could not find or load main class com.ComPac. Caused by: java.lang.ClassNotFoundException: com.ComPac . Error: Could not find or load main class com.ComPac. Caused by: java.lang.ClassNotFoundException: com.ComPac

So how do I run this?那么我该如何运行呢? and what exactly is the logic for running when it comes to packages in java? java 中的封装运行的具体逻辑是什么?

Java used- openjdk version "11.0.8" 2020-07-14 LTS(AWS Corretto) Java used- openjdk version "11.0.8" 2020-07-14 LTS(AWS Corretto)

OS used- Amazon Linux 2使用的操作系统 - Amazon Linux 2

put the class in a folder called "com"将 class 放在名为“com”的文件夹中

in a bash shell it's then:在 bash shell 中它是:

$ java com/ComPac

(from the folder containing "com", not inside of "com") (来自包含“com”的文件夹,而不是“com”内部)

If you are using Java 11 then you don't need to first compile java file and then run the class file.如果您使用的是Java 11那么您不需要先编译 java 文件,然后运行class文件。 You can directly run the java file using just您可以直接使用 java 文件

java.\test.java

test.java测试.java

package com;

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

command:命令:

java.\test.java

Output: Output:

Hello World

The correct way of doing it as follows:正确的做法如下:

javac -d . ComPac.java

The switch -d.开关-d. asks the compiler to place generated class files at the current directory as .要求编译器将生成的 class 文件作为. stands for the current directory.代表当前目录。 Learn more about the switches by simply using the command javac只需使用命令javac即可了解有关开关的更多信息

Now, if you use the command ls in Mac/Unix or dir in Windows, you will see a directory, com has been created and ComPac.class has been placed inside this directory.现在,如果您在 Mac/Unix 中使用ls命令或在 Windows 中使用dir命令,您将看到一个目录, com已创建,并且ComPac.class已放置在此目录中。 In order to execute the class, you can now use the following command:为了执行 class,您现在可以使用以下命令:

java com.ComPac

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

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