Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
194 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 . comexamplemodel*.java comexampleweb*.java
java -cp . com.example.web.Bear 

and the following is being printed to the console:

Yay, it works!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...