0

Test.java

public class Test{
    public static void main(String []args){
        Student student = new Student();
        student.showData();
    }
}

Student.java

class Student{
    private int score = 100;
    private String name = "Alex";
    public void showData(){
        System.out.printf("name: %s score: %s\n", name, score);
    }
}

when I Compile the code above I always get a error say

.\Test.java:3: error: cannot find symbol
                Student student = new Student();
                ^
  symbol:   class Student
  location: class Test
.\Test.java:3: error: cannot find symbol
                Student student = new Student();
                                      ^
  symbol:   class Student
  location: class Test
2 errors

but when I copy the code from Student into Test and it work. So I want to know why can Someone help me? please. I using cmd from windows11

PS D:\test> ls


    目錄: D:\test


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----      2021/11/18  下午 09:44            637 Student.class
-a----      2021/11/18  下午 06:30            165 Student.java
-a----      2021/11/18  下午 09:48            128 Test.java


PS D:\test> javac .\Student.java
PS D:\test> javac .\Test.java
.\Test.java:3: error: cannot find symbol
                Student student = new Student();
                ^
  symbol:   class Student
  location: class Test
.\Test.java:3: error: cannot find symbol
                Student student = new Student();
                                      ^
  symbol:   class Student
  location: class Test
2 errors
IanShen
  • 1
  • 1
  • 4
    Does this answer your question? [What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean) – maloomeister Nov 18 '21 at 10:42
  • How do you start the compilation? From an IDE? From a command line? How exactly (e.g. command line used, IDE project settings, ...)? Please show us. – Ralf Kleberhoff Nov 18 '21 at 10:46
  • PS D:\test> ls 目錄: D:\test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2021/11/18 下午 09:44 637 Student.class -a---- 2021/11/18 下午 06:30 165 Student.java -a---- 2021/11/18 下午 09:48 128 Test.java – IanShen Nov 18 '21 at 13:48
  • I use the cmd which from my windows11 @RalfKleberhoff PS D:\test> javac .\Student.java PS D:\test> javac .\Test.java .\Test.java:3: error: cannot find symbol Student student = new Student(); ^ symbol: class Student location: class Test .\Test.java:3: error: cannot find symbol Student student = new Student(); – IanShen Nov 18 '21 at 13:53
  • btw when I use IDE it also work! error only show up when I use the cmd – IanShen Nov 18 '21 at 14:33
  • If you REALLY want to do it from the command line, use `javac *.java`, meaning to compile all sources in a combined run. Then javac sees the `Student` class when compiling `Test.java`. But easier: let the IDE compile your program. – Ralf Kleberhoff Nov 18 '21 at 15:03
  • Well it didn't work but thank you guys trying to help me to solve my problem I think maybe is my computer have some kind of problem! thank you! – IanShen Nov 18 '21 at 15:45
  • Maybe check that your classpath has current working directory ( . ) in it. – tnavidi Nov 19 '21 at 12:53

0 Answers0