27

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?

Coding Mash
  • 3,338
  • 5
  • 24
  • 45
andrian
  • 423
  • 1
  • 8
  • 15
  • 1
    Why have you posted the source for your classes? How is it relevant to your question? Please read [SSCCE](http://SSCCE.org) – Bohemian Nov 24 '12 at 02:20
  • What classpath have you used for the compilation? – Daniel Fischer Nov 24 '12 at 02:21
  • Are you compiling from `src` ? – Prince John Wesley Nov 24 '12 at 02:22
  • I try to compile in many path before, except "src". I just tried to delete import command then compile from "src", and it works! well thanks.. – andrian Nov 24 '12 at 06:18
  • 9
    @Bohemian - I'm sure glad he posted them, at least that makes it "Self Contained, Compilable, Example" - what would you have done? Just posted "src/vehicles/BicycleMain.java:6: cannot find symbol "? – sdaau Mar 04 '14 at 19:38
  • @sdaau ideally, post the smallest amount of code that causes the error, and the error message, so others can simply copy-paste into their IDE and reproduce the problem themselves. – Bohemian Mar 04 '14 at 20:04
  • Tink solved that one for reproducal ones recently and provides links. Sounds like. – Oleksii Kyslytsyn Feb 20 '22 at 18:56

5 Answers5

36

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.

Sushant Kr
  • 1,953
  • 14
  • 19
  • 1
    Alternatively, go into your `src` directory (the one containing the `vehicles` directory), compile using `javac vehicles/BicycleMain.java`, and run using `java vehicles.BicycleMain`. – Kurt Peek Dec 23 '18 at 08:02
10

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

Extreme Coders
  • 3,441
  • 2
  • 39
  • 55
  • this my fault to compile in incorrect folder, I deleted the import command and try to compile in "src", it works! thanks... – andrian Nov 24 '12 at 06:20
2

I have solved this problem, compiling from "src".

Something like this: javac ./my_folder/my_file.java

Kind Regards

1

I tried all the solutions here but eventually I found that the problem for me was I was using different versions of the JDK and JRE.

Check JRE version:

java -version

Check JDK version:

javac -version
Paymon Wang-Lotfi
  • 545
  • 2
  • 11
  • 29
-1

Just remove the package line from beginning and it'll work 100%.

Go to the folder in which files are stored via terminal and type javac *.java

There will be no need to import classes too.

Praveen Rana
  • 37
  • 1
  • 8