简体   繁体   English

Javac“找不到符号”

[英]Javac "cannot find symbol"

I've the root directory like this :我有这样的根目录:

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

Bicycle.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自行车主程序

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 :我编译了 Bicycle.java 并成功了,但不是 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!我尝试使用 Netbeans 和 IT WORKS 运行这些文件! but why it doesn't work when I compile in CLI?但是为什么当我在 CLI 中编译时它不起作用?

First, To compile the java source file using javac you need to specify the files to compile explicitly.首先,要使用javac编译 java 源文件,您需要明确指定要编译的文件。

Example:例子:

javac PathToSourceFile/FileName.java

you need not provide the path if the source file is in the current working directory.如果源文件在当前工作目录中,则不需要提供path

Second, whenever java encounters import abc.xyz.ClassName;其次,每当java遇到import abc.xyz.ClassName; it tries to resolve abc/xyz/ClassName with respect to the classpath or current working directory.它尝试根据classpath或当前工作目录解析abc/xyz/ClassName

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.这将解决您的Bicycle课程。

and then use然后使用

java -cp ../ vehicles/BicycleMain to run. java -cp ../ vehicles/BicycleMain来运行。

Try deleting the line import vehicles.*;尝试删除线import vehicles.*; from BicycleMain.java and them compiling with javac in command line.来自BicycleMain.java并在命令行中使用 javac 进行编译。

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.*;顺便说一下,这是因为当你在 javac 中编译时,你在文件夹vehicles ,你写了一条语句import vehicles.*; in BicycleMain.java which means to the compiler there is another folder vehicles within the vehicles folder which is not the case hereBicycleMain.java ,这对编译器来说意味着在vehicles文件夹中还有另一个文件夹vehicles ,这里不是这种情况

I have solved this problem, compiling from "src".我已经解决了这个问题,从“src”编译。

Something like this: javac ./my_folder/my_file.java像这样: javac ./my_folder/my_file.java

Kind Regards亲切的问候

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.我在这里尝试了所有解决方案,但最终我发现问题在于我使用了不同版本的 JDK 和 JRE。

Check JRE version:检查 JRE 版本:

java -version

Check JDK version:检查JDK版本:

javac -version

Just remove the package line from beginning and it'll work 100%.只需从一开始就删除包装线,它就会 100% 工作。

Go to the folder in which files are stored via terminal and type javac *.java通过终端转到存储文件的文件夹并键入javac *.java

There will be no need to import classes too.也不需要导入类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM