Javac“找不到符号" [英] Javac "cannot find symbol"

查看:46
本文介绍了Javac“找不到符号"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的根目录:

├── classes
└── src
    └── vehicles
        ├── Bicycle.java
        └── BicycleMain.java

自行车.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

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());
        } }

我编译了 Bicycle.java 并且成功了,但不是用于 BicycleMain.java :

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

我尝试使用 Netbeans 运行这些文件,并且可以正常工作!但是为什么我在 CLI 中编译时它不起作用?

I try to run these files with Netbeans and IT WORKS! but why it doesn't work when I compile in CLI?

推荐答案

首先,要使用 javac 编译 java 源文件,您需要明确指定要编译的文件.

First, To compile the java source file using javac you need to specify the files to compile explicitly.

示例:

javac PathToSourceFile/FileName.java

如果源文件在当前工作目录中,则不需要提供path.

you need not provide the path if the source file is in the current working directory.

其次,每当 java 遇到 import abc.xyz.ClassName; 时,它都会尝试根据 classpath 解析 abc/xyz/ClassName或当前工作目录.

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

它应该编译,因为类路径将被设置为包含车辆的目录(../).这将解析您的 Bicycle 类.

and it should compile, because classpath will be set to the directory(../) containing vehicles. which will resolve your Bicycle class.

然后使用

java -cp ../Vehicles/BicycleMain 运行.

这篇关于Javac“找不到符号"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆