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?
This works:
package com.example.model;
public class BearExtra {
public static void go() {
System.out.println("Yay, it works!");
}
}
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:
javac -cp . com/example/model/*.java com/example/web/*.java
java -cp . com.example.web.Bear
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With