Interface Implementation error : cannot find symbol

12,801

Solution 1

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.

Solution 2

First of all as your package name is test you must keep your class and the interface in a folder named test.

Second thing since they are in the same folder named test remove import test.MotorVehicle; from the class defination

Suppose if your folder test resides in g:/ such that g:/test/contains class and the interface.

Then try opening the command prompt in g:/

then type the following commands

for compiling

javac test/Car.java

and for executing

 java test.Car

Though you may get Error: Main method not found in class test.Car as your class does not contain main mathod

Solution 3

You are going in to exact path by the use of cd command.Because of that interface is not accessible as class will try to find out it from package from current/running location.

For make this compile you have to specify fully (again Fully) qualified name of package during compilation.

For Example

If you class is in a.b.test package compile it like this

javac a/b/test/Car.java
Share:
12,801
misguided
Author by

misguided

Novice Programmer !!!

Updated on June 05, 2022

Comments

  • misguided
    misguided almost 2 years

    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.

    enter image description here

  • misguided
    misguided almost 10 years
    javac is working fine , as I am able to compile other java files successfully .Is setting classpath still required?
  • shazin
    shazin almost 10 years
    He is not trying to Run and Car doesn't have a main method
  • shazin
    shazin almost 10 years
    Yes, you need to set the classpath to point to the package test before compiling car.
  • shazin
    shazin almost 10 years
    @SotiriosDelimanolis I am not talking about source code but compiled .class files. Sorry for the misunderstanding, please look at the edit.
  • misguided
    misguided almost 10 years
    Thanks for explaining . Clears up the whole thing now. COmplining fine now , after implementing your suggestion. I liked the simple language in which you have explained , hence accepted the answer