package does not exist error!

旧街凉风 提交于 2019-12-28 06:21:45

问题


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 BearExtra.java in directory structure com/example/model in same root directory as above. I am calling a method in BearExtra.java from Bear.java and I am getting the error that the package does not exist.

I have imported com.example.model package in my java file. Can give me some advice?


回答1:


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!



回答2:


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 { ...


来源:https://stackoverflow.com/questions/6665420/package-does-not-exist-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!