I've the root directory like this :
├── classes └── src └── vehicles ├── Bicycle.java └── BicycleMain.java
Bicycle.java
package vehicles; public class Bicycle { public int cadence; public int gear; public int speed; public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void setSpeed(int newValue) { speed = newValue; } public int getGear() { return gear; } public int getCadence() { return cadence; } public int getSpeed() { return speed; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; }
BicycleMain.java
package vehicles; import vehicles.*; public class BicycleMain { public static void main (String args[]){ Bicycle Bike = new Bicycle(10, 20, 1); System.out.println("We have a new bicycle with speed = " +Bike.getSpeed()+", cadence = "+Bike.getCadence()+", gear = "+Bike.getGear()); } }
I compiled the Bicycle.java and successful, but not for BicycleMain.java :
symbol : class Bicycle location: class vehicles.BicycleMain Bicycle Bike = new Bicycle(10, 20, 1); ^ src/vehicles/BicycleMain.java:6: cannot find symbol symbol : class Bicycle location: class vehicles.BicycleMain Bicycle Bike = new Bicycle(10, 20, 1); ^ 2 errors
I try to run these files with Netbeans and IT WORKS! but why it doesn't work when I compile in CLI?
In the above program, "Cannot find symbol" error will occur because “sum” is not declared. In order to solve the error, we need to define “int sum = n1+n2” before using the variable sum.
The “cannot find symbol” error comes up mainly when we try to use a variable that's not defined or declared in our program. When our code compiles, the compiler needs to verify all the identifiers we have. The error “cannot find symbol” means we're referring to something that the compiler doesn't know about.
javac is not recognized is an error occurs while we compile the Java application. It is because the JVM is unable to find the javac.exe file. The javac.exe file is located in the bin folder of the JDK. The reason behind to occur the error is that the PATH is not added to the System's environment variable.
First, To compile the java source file using javac
you need to specify the files to compile explicitly.
Example:
javac PathToSourceFile/FileName.java
you need not provide the path
if the source file is in the current working directory.
Second, whenever java encounters import abc.xyz.ClassName;
it tries to resolve abc/xyz/ClassName
with respect to the classpath
or current working directory.
So if you are inside the vehicles folder and compile your code, it wont compile because it will look for folder vehicles inside folder vehicles (which doesn't exist!).
but, you can do this when inside the vehicles folder
javac -cp ../ BicycleMain.java
and it should compile, because classpath will be set to the directory(../
) containing vehicles. which will resolve your Bicycle
class.
and then use
java -cp ../ vehicles/BicycleMain
to run.
Try deleting the line import vehicles.*;
from BicycleMain.java
and them compiling with javac in command line.
By the way it happens because while you are compiling in javac you are in the folder vehicles
and you write a statement import vehicles.*;
in BicycleMain.java
which means to the compiler there is another folder vehicles
within the vehicles
folder which is not the case here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With