我在尝试在命令行上运行我的程序时遇到了一些问题。我在我的 NetBeansProjects 目录中创建了 2 个目录。第一个称为foodpackage,第二个是foodpackage 的子目录,称为fruitpackage。在fruitpackage 中,我有两个名为Food.java 和FoodMain.java 的java 文件。Food.java中的代码如图所示:
package foodpackage.fruitpackage;
public class Food{
// declare instance variabels
private String foodVarieties;
private String foodList;
// returns foodVarieties
public String getFoodVarieties(){
return foodVarieties;
}
// sets foodVarieties
public void setFoodVarieties(String temp){
foodVarieties = temp;
}
// returns foodList
public String getFoodList(){
return foodList;
}
// sets foodList
public void setFoodList(String temp){
foodList = temp;
}
}
FoodMain.java的代码如下所示:
import foodpackage.fruitpackage.*;
public class FoodMain{
public static void main(String args[]){
// create an object instance of Food class
Food t = new Food();
// set the foodVarieties
t.setFoodVarieties("Food varieties: Vegetables and Meat");
// set the foodList
t.setFoodList("Food list: chicken, lettuce, and broccoli");
// print the foodVarieties
System.out.println(t.getFoodVarieties());
// print the foodList
System.out.println(t.getFoodList());
}
}
我在命令行上运行它,如下所示:
cd /Users/timothylee/NetBeansProjects
javac -cp . foodpackage/fruitpackage/*.java
java -classpath . foodpackage/fruitpackage.FoodMain
当我运行这个时,我得到了错误
Exception in thread "main" java.lang.NoClassDefFoundError: foodpackage/fruitpackage/FoodMain (wrong name: FoodMain)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
有人可以帮我解决这个错误吗?谢谢你。