Issue
I am implementing the following sample interface:
package test1;
public interface MotorVehicle {
void run();
int getFuel();
}
In the class
package test1;
import test1.MotorVehicle;
public class Car implements MotorVehicle
{
int fuel;
public void run(){
System.out.println("Running");
}
public int getFuel(){
return this.fuel;
}
}
When I try to compile the class file , I get the following error :
Car.java:4: error: cannot find symbol
public class Car implements MotorVehicle
^
symbol: class MotorVehicle
1 error
Compile Steps:
Step:1 javac MotorVehicle.java
Step:2 javac Car.java
Both my interface and the class are in the same directory , why does ut come up with cannot find symbol error?
Edit: As suggested , have changed the package , and tried to run the same code again . Still getting an error.
Solution
The problem is that you're in the wrong folder when compiling.
From the console screenshot, it is clear that you are inside /test1
. However, the package test1;
statement expects a folder inside the current folder named test1
. It can't find that folder/package, so you get an error.
The solution is to go up one folder, so you end up in /src
, then compile using the path to the file, e.g. javac test1/Car.java
. Explanation: You are in the folder /src
, the package
statement inside the classes says they are inside the folder test1
which is inside /src
. Now every package/path can be resolved.
And you shouldn't import
things that are in the same package.
Answered By - blueygh2