Javac “cannot find symbol”

前端 未结 5 570
眼角桃花
眼角桃花 2020-11-30 10:57

I\'ve the root directory like this :

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

Bicycle.jav

相关标签:
5条回答
  • 2020-11-30 11:34

    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
    
    0 讨论(0)
  • 2020-11-30 11:38

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

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

    Kind Regards

    0 讨论(0)
  • 2020-11-30 11:40

    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.

    0 讨论(0)
  • 2020-11-30 11:49

    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

    0 讨论(0)
  • 2020-11-30 11:52

    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.

    0 讨论(0)
提交回复
热议问题