0

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.

6
  • Please, do not upload images of code/data/error. Commented Sep 7, 2023 at 3:03
  • Show us the other file. Then comment here once you edited your question. Commented Sep 7, 2023 at 3:41
  • How exactly are you compiling the classes? Commented Sep 7, 2023 at 6:45
  • 1
    @davidalayachew I have re-edited my question Commented Sep 7, 2023 at 7:31
  • @Jesper I dont actually know what is compiling in Java. Do you meant the directories of the files? I put both the Main.java & Organ.java under OrganProject folder. Commented Sep 7, 2023 at 7:33

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

6 Comments

Hi!!! Thank you so much for your suggestion. My codes worked after using ur method. However, if I stop using javac in the terminal, and choose to run the Main.java file in VScode, the same error comes along.
@ling131031003 I am glad it worked. And yes, that makes sense why it doesn't work. If you want, you can run the commands I showed you in VSCode's terminal. That should work just fine as well.
But remember, in Java, you are always supposed to compile your code before running it. javac is the command that compiles code, and java is the command that runs it. So, if you do not call javac, then you will not be able to run your code. That said, if your code is only in 1 file, and not 2 or more, then you can get away with just calling java on your Main.java. In the near future, they are actually going to let you get away with running multiple files without compiling them. Here is a link to that project proposal. openjdk.org/jeps/8304400
And please note, if the version of Java you are running is Java 11 or later, then that explains why you are running into the problem you did. They have not yet released the ability to run multiple files without compiling, but Java 11 is when they released the ability to run a single file without compiling. Here is a link to the single file source code proposal. openjdk.org/jeps/8304400 -- Notice how they restrict this ability to a single file. And notice how the previous link I sent you talks about how they intend to deal with the exact problem you are describing.
Thank you!!! I really appreciate your help! It really helps me a lot as I was bothered by this problem for quite a long time. Anyways, thank you so much again for your kindness.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.