Reputation: 3789
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.
Upvotes: 2
Views: 17833
Reputation: 69
After Compiling Motorvehicle.java. you have to create a folder test1 and transfer the MotorVehicle.class into the folder test1 then compile the next file Car.java. This will solve your error
Upvotes: 0
Reputation: 22972
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
Upvotes: 1
Reputation: 21883
First compile MotorVehicle
as it doesn't have any dependencies. Then set the classpath
Before issuing javac Car.java
compile statements you need to set the Classpath
Windows
set CLASSPATH=%CLASSPATH%;<PATH_TO_COMPILED_BINARY>/
Unix
export CLASSPATH=$CLASSPATH:<PATH_TO_COMPILED_BINARY>/
<PATH_TO_COMPILED_BINARY>
should not include the package test1
Example :
C:/sourcecode/test1
Then <PATH_TO_COMPILED_BINARY>
should be C:/sourcecode
Update
Removing the import test1.MotorVehicle
will also fix the issue.
Upvotes: 0
Reputation: 1538
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.
Upvotes: 11
Reputation: 8946
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
Upvotes: 2