Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stuck when studying java from learnjavaonline

class Student {
    private String firstName;
    private String lastName;
    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    
    public void printFullName() {
        System.out.println (this.firstName+" "+this.lastName);
    }
}

public class Main {
    public static void main(String[] args) {
        Student[] students = new Student[] {
            new Student("Morgan", "Freeman"),
            new Student("Brad", "Pitt"),
            new Student("Kevin", "Spacey")
        };
        for (Student s : students) {
            s.printFullName();
        }
    }
}

I was learning java from learnjavaonline and this is the answer for one of the exercises. In their website the code runs without anyproblem, but when I ran in VScode (Java extension pack was installed and so was the latest version of Java) it returned me with the error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at Main.main(Student.java:15)

So can someone help me by pointing out what is the problem?

like image 845
Forever Beginner Avatar asked Dec 28 '25 22:12

Forever Beginner


1 Answers

The code is perfectly fine. Just rename the file as Main.java and use some other IDE. I feel this might be some setting issue with VSCode. You have to resolve all workspace errors and make sure there is only one main(String args[]) function in your file.I too faced problem compiling a java file using java extension pack, it always showed build error but when click proceed it works. I strongly recommend you try eclipse. Or try using command line.

  • Go to the folder location where you file is located.
  • type javac Main.java
  • It will compile successfully. To run use java -c . Main
  • Make sure java is in you system path variable.
like image 91
Mukesh Verma Avatar answered Dec 30 '25 10:12

Mukesh Verma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!