package does not exist error!

后端 未结 2 642
遇见更好的自我
遇见更好的自我 2020-12-06 02:07

I have a directory structure like com/example/web under the root directory which contains a java file Bear.java. I have another java file Bea

相关标签:
2条回答
  • 2020-12-06 02:36

    This works:

    com/example/model/BearExtra.java

    package com.example.model;
    
    public class BearExtra {
      public static void go() {
        System.out.println("Yay, it works!");
      } 
    }
    

    com/example/web/Bear.java

    package com.example.web;
    
    import com.example.model.*;
    
    public class Bear {
      public static void main(String[] args) {
        BearExtra.go();
      }
    }
    

    Now, to compile and run these classes, go to the directory where you can "see" the com folder and do:

    *nix/MacOS

    javac -cp . com/example/model/*.java com/example/web/*.java
    java -cp . com.example.web.Bear 
    

    Windows

    javac -cp . com\example\model\*.java com\example\web\*.java
    java -cp . com.example.web.Bear 
    

    and the following is being printed to the console:

    Yay, it works!
    
    0 讨论(0)
  • 2020-12-06 02:47

    Did you specify the 'package' in your class files?

    Bear.java

    package com.example.web;
    
    import com.example.model.*;
    
    class Bear { ...
    

    BearExtra.java

    package com.example.model;
    
    public class BearExtra { ...
    
    0 讨论(0)
提交回复
热议问题