英文:
Getting java.lang.ClassNotFoundException when using package
问题
我有一个名为ComPac.java
的Java文件,其中包含以下代码:
package com;
public class ComPac {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
该文件位于路径:/home/ec2-user/java_c
要编译此文件,我运行了javac Compac.java
,并生成了类文件。
现在轮到运行类文件了。
所以我运行了java ComPac
(如下所示的截图):
理所当然地,我收到了错误消息Error: Could not find or load main class ComPac. Caused by: java.lang.NoClassDefFoundError: com/ComPac (wrong name: ComPac)
。我假设这是因为Java文件中声明了com
包。
因此,我尝试了java com.ComPac
,并期望它可以工作(如下所示的截图)。
但是我收到了错误消息:Error: Could not find or load main class com.ComPac. Caused by: java.lang.ClassNotFoundException: com.ComPac
。
那么我应该如何运行它?以及在Java中涉及包时运行的逻辑是什么?
Java版本:openjdk version "11.0.8" 2020-07-14 LTS (AWS Corretto)
操作系统:Amazon Linux 2
英文:
I have a java file ComPac.java
with the below code:
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
To compile this file, I ran javac Compac.java
, and the class file was generated.
Now its turn to run the class file.
So I did java ComPac
(screenshot below)
Understandably, I got the error 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.
So instead I tried, java com.ComPac
and expected it to work(screenshot below).
But I got the error: 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 used- openjdk version "11.0.8" 2020-07-14 LTS(AWS Corretto)
OS used- Amazon Linux 2
答案1
得分: 5
将类放入名为“com”的文件夹中。
在bash shell中,然后是:
$ java com/ComPac
(从包含“com”文件夹的位置运行,而不是在“com”内部)。
英文:
put the class in a folder called "com"
in a bash shell it's then:
$ java com/ComPac
(from the folder containing "com", not inside of "com")
答案2
得分: 3
如果您正在使用Java 11
,则无需先编译Java文件,然后运行class
文件。您可以直接运行Java文件,只需使用以下命令:
java .\test.java
> test.java
package com;
public class test {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
命令:
java .\test.java
输出:
Hello World
英文:
If you are using Java 11
then you don't need to first compile java file and then run the class
file. You can directly run the java file using just
java .\test.java
> test.java
package com;
public class test{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
command:
java .\test.java
Output:
Hello World
答案3
得分: 2
以下是翻译好的部分:
正确的做法如下:
javac -d . ComPac.java
开关 -d .
会要求编译器将生成的类文件放在当前目录,.
代表当前目录。您可以通过使用命令 javac
来了解更多关于这些开关的信息。
现在,如果您在Mac/Unix中使用 ls
命令或在Windows中使用 dir
命令,您会看到一个名为 com
的目录已经创建,而且 ComPac.class
已经放在这个目录中。为了执行这个类,您现在可以使用以下命令:
java com.ComPac
英文:
The correct way of doing it as follows:
javac -d . ComPac.java
The switch -d .
asks the compiler to place generated class files at the current directory as .
stands for the current directory. Learn more about the switches by simply using the command 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. In order to execute the class, you can now use the following command:
java com.ComPac
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论