search

Home  >  Q&A  >  body text

shell - 用命令行对javac进行编译,总是『cannot find symbol』,但是eclipse执行不会出现问题

代码如下
分别是enum和testclass两个java文件

package cs121assignment1;

public enum Food {
    APPLE("fruit", 55),
    BANANA("fruit", 80),
    CARROT("vegetable", 60);
    
    private final String catagory; //vegetable or fruit
    private final int calorie;
    
    Food(String catagory, int calorie){
        this.catagory = catagory;
        this.calorie = calorie;
    }
    
    public int getCalorie(){
        return calorie;
    }
    
    public String getCatagory(){
        return catagory;
    }
}
package cs121assignment1;

public class TestFood {

public static void main(String[] args){
    System.out.println("All foods:");
    
    for(Food food : Food.values()){
        System.out.printf("%s, catagory: %s, calorie: %d kilocalorie each\n", food, food.getCatagory(), food.getCalorie());
    }
        
}

eclipse中运行结果如下:

但是用命令行执行javac的时候显示如下:

阿神阿神2806 days ago700

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-18 10:50:47

    cd Desktop;
    javac cs121assignment1.TestFood;

    不要
    cd Desktop/cs121assignment1;

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:50:47

    Delete all the package statements in the first line of the code
    cd to the folder containing the .java file
    Execute javac *.javajavac *.java
    得到两个.class文件
    java TestFoodGet two .class files
    java TestFood

    will run successfully


    This problem is actually a problem with the usage of package
    Another method is not to remove the package in the first line of the filejavac -d ~/Desktop/cs121assignment1 Food.java TestFood.java
    会生成一个以cs121assignment1为命名的新文件夹包含Food.class 和 TestFood.class
    在新生成的cs121assignment1的上层目录用java cs121assignment1.TestFoodjavac -d ~/Desktop/cs121assignment1 Food.java TestFood.java

    It will generate a file ending with The new folder named cs121assignment1 contains Food.class and TestFood.class🎜Use java in the upper directory of the newly generated cs121assignment1 cs121assignment1.TestFood will run successfully🎜

    reply
    0
  • Cancelreply