Search code examples
javacompiler-errorspackagecannot-find-symbol

Java: cannot find symbol when files are in same folder


I created a file Organ.java

package OrganProject;

public class Organ{

    private String name;
    private String condition;

    public Organ(String name, String condition){

        this.name = name;
        this.condition = condition;

    }

    public String getName(){
        return this.name;
    }

    public String getCondition(){
        return this.condition;
    }

    public void setName(String name){
        this.name = name;
    }

    public void setCondition(String condition){
        this.condition = condition;
    }

    public void display(){
        System.out.println("Name: " + this.getName());
        System.out.println("Condition: " + this.getCondition());
    }
}

however, when I call this class in Main.java

package OrganProject;
public class Main { 
    public static void main(String[] args) {
        Organ organ = new Organ("eye", "good");
        organ.display();
    }
}

There was an error showing that

Main.java:7: error: cannot find symbol
        Organ organ = new Organ("eye", "good");
        ^
  symbol:   class Organ
  location: class Main
Main.java:7: error: cannot find symbol
        Organ organ = new Organ("eye", "good");
                          ^
  symbol:   class Organ
  location: class Main
2 errors

The 2 files are all in the same folder called OrganProject; however, they seem unable to interlink each other. May I ask what is the reason behind it and how to resolve this problem?

I had tried to create new folder and move the files into that new folder, hope can refresh the compile, however, it did not work.


Solution

  • In your terminal, make a folder called OrganProject, then put your 2 files into there. Then, go outside the folder and call the following command in that terminal.

    javac OrganProject/Main.java OrganProject/Organ.java
    

    This compiles your code, creating corresponding .class files. Then, while still being immediately outside of the OrganProject folder, call the following command in that terminal too.

    java OrganProject/Main
    

    This will actually run the code. When I run your code, this is what I get.

    Name: eye
    Condition: good