I am having problems understanding how packages work. Not using any IDE.
I have the following folder structure:
~/dropbox/api/data
Inside the data folder, there are (only) Producer.java, Consumer.java that look like:
package data;
public class Producer {
public static void main(String[] args) {
//do some production
}
public static Producer getProducer() {
//call constructor and return instance
}
}
----------------
package data;
public class Consumer {
public static void main(String[] args) {
Producer worker = getProducer();
}
}
I cd
to data folder and javac Producer.java
to compile Producer.class in the same folder. Then when I run java Producer
, I get Error: Could not find or load main class Producer
Caused by: java.lang.NoClassDefFoundError: Producer (wrong name: data/Producer).
Same error, when I tried java Producer
and java data.Producer
from the parent api
folder, and java data.Producer
in data
folder.
- What’s wrong here? If I remove the
package data;
, it compiles (javac) and runs (java) fine in data folder.
Also, when I tried javac Consumer.java
, I get error: cannot find symbol Producer. They are in the same folder and declared same package.
- What’s wrong here?