Issue
I have created a project with the Maven directory structure and made a package folder called machine
in src/main/java/machine
.
In the class source file src/main/java/machine/MyClass.java
I have defined the package as follows at the top of the file:
package main.java.machine;
However, when I try to compile my Main.java file which is outside of the package with the statement
import main.java.machine.MyClass;
I get a compile error "symbol not found" when i try to instantiate an object from MyClass.
I don't understand how I can follow the naming convention of naming packages after their directory path, while at the same time naming them after the enclosing folder (in my case that would be package machine;
, which works).
I tried doing the same thing in a real Maven project with IntelliJ IDEA, and the IDE prompts me to either create a new package folder with the name java.machine.MyClass
or rename the package to machine
in the statement at the top of the MyClass.java file.
Thanks a lot
Solution
The Maven default source folder is src/main/java/
, so all your packages must be included in that folder. Instead, the one related to test, should be included in src/main/test
.
For example, in your case, if you have created the package machine, your class under that package must have the following statement:
package machine;
Instead, all classes that want to import the classes inside machine package will import it in this way:
import machine;
However, as best practice, try to start a package name with a "domain", followed by the name of your project, then you can differentiate each package by its own purpose. For example, a classic package coud be com.myproject.machine
Answered By - Marco Tizzano
Answer Checked By - Robin (JavaFixing Admin)