java在命令行上

时间:2013-07-11 21:05:40

标签: java command-line package

我在尝试在命令行上运行程序时遇到了一些问题。我在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)

愿任何人帮我解决此错误吗?谢谢。

4 个答案:

答案 0 :(得分:6)

使用+代替=来连接Strings

System.out.println("ID: " + annaID.getStudentID());

答案 1 :(得分:1)

您的专线目前是:

System.out.println("ID: " = annaID.getStudentID());

=不是您正在寻找的运营商。

System.out.println("ID: " + annaID.getStudentID());

这会将String与annaID.getStudentID()

返回的整数连接起来

答案 2 :(得分:0)

执行此操作时:

System.out.println("ID: " = annaID.getStudentID());

您正在尝试将int分配给字符串,这很糟糕。请改用+运算符:

System.out.println("ID: " + annaID.getStudentID());

答案 3 :(得分:0)

检查您的代码...按照您提供的图像,看起来您在system.out.println语句中键入了“=”而不是“+”