【发布时间】:2021-12-09 01:27:32
【问题描述】:
我在 helloworld 包中有 HelloWorld 程序:
package helloworld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
我把这个HelloWorld.java文件放在helloworld包文件夹中,该文件夹在myapp文件夹中,如下:
C:\test\myapp>dir
Volume in drive C has no label.
Volume Serial Number is F48A-3578
Directory of C:\test\myapp
10/22/2021 07:28 PM <DIR> .
10/22/2021 07:28 PM <DIR> ..
10/22/2021 07:28 PM <DIR> helloworld
0 File(s) 0 bytes
3 Dir(s) 12,348,715,008 bytes free
C:\test\myapp>cd helloworld
C:\test\myapp\helloworld>dir
Volume in drive C has no label.
Volume Serial Number is F48A-3578
Directory of C:\test\myapp\helloworld
10/22/2021 07:28 PM <DIR> .
10/22/2021 07:28 PM <DIR> ..
10/22/2021 07:16 PM 436 HelloWorld.class
10/22/2021 07:14 PM 148 HelloWorld.java
2 File(s) 584 bytes
2 Dir(s) 12,348,715,008 bytes free
C:\test\myapp\helloworld>
在 CMD (Windows) 中,我位于 myapp 文件夹上方的文件夹中,因此,我可以将其编译为:
C:\test>javac myapp\helloworld\HelloWorld.java
C:\test>
但是,当我尝试如下运行它时,它失败了。
C:\test>java myapp\helloworld.HelloWorld
Error: Could not find or load main class myapp\helloworld.HelloWorld
Caused by: java.lang.NoClassDefFoundError: helloworld/HelloWorld (wrong name: myapp\helloworld/HelloWorld)
当然,如果我进入 myapp 文件夹,如下所示,我可以运行它。
C:\test>cd myapp
C:\test\myapp>java helloworld.HelloWorld
Hello World
C:\test\myapp>
我的问题是如何运行位于不属于包的子文件夹中的类文件?
【问题讨论】:
标签: java class cmd package subdirectory