gpt4 book ai didi

java - ClassNotFoundException Sandwich.Java...它在哪里?

转载 作者:行者123 更新时间:2023-12-01 15:53:55 25 4
gpt4 key购买 nike

好的,我在根文件夹中有一个名为“Sandwich.java”的文件,在 [root]/MyFrstPkg 的文件夹内有一个名为“SandwichType.java”的文件。无论出于何种原因,它都不会编译,并声称找不到 Sandwich.java。这是目录结构:

root --
|
|- Sandwich.java
|
|-MyFirstPkg
|
|-SandwichType.java

这是 Sandwich.java:

//note I also tried adding package MyFrstPkg; in this file as well and removing the leading MyFrstPkg. from the import statement below, still no luck.

import MyFrstPkg.SandwichType; //the text 'MyFrstPkg' part is underlined as an error

class Sandwich{

SandwichType type; //the text 'SandwichType' is underlined as an error

public static void main(String[] args){

Sandwich sndwch1 = new Sandwich();

sndwch1.type = SandwichType.HAM; //the text 'SandwichType' is underlined as an error

System.out.println("A HAM costs $"+sndwch1.type.getCost());
System.out.println("and has "+ sndwch1.type.getSlices()+" slices.");
}

}

这是 SandwichType.java:

package MyFrstPkg;

enum SandwichType{

HAM(0,0f);

SandwichType(int numSlices, float cost){ // constructor - Ryan changed 'numslices' to 'numSlices'
this.numSlices = numSlices;
this.cost = cost;
} //end constructor

private int numSlices; //These are specific to this
private float cost; // enum class...

public int getSlices(){
return numSlices;
}

public float getCost(){
return cost;
}

}//end of SandwichType enum

我在 CMD 中浏览到根位置并运行“java Sandwich.java”,我得到的只是 ClassNotFoundExeption Sandwich.Java,为什么找不到它?它本身 D:

最佳答案

要编译您的类,请使用

javac Sandwich.java

如果没有给出错误消息,您应该可以调用

java Sandwich

启动您的程序。

<小时/>

如果第一个工作没有错误,我们就更进一步了。如果第二个不起作用,请尝试以下方法:

java -cp . Sandwich

如果这样工作,则说明您设置了错误的类路径。输入 echo %CLASSPATH% 并发布结果。 (通常,对于简单的项目,您根本不需要 CLASSPATH 变量。)

关于java - ClassNotFoundException Sandwich.Java...它在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5453369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com