Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

java - Javac "cannot find symbol"

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...