Home > database >  Why is my VS-CODE unable to find a main method?
Why is my VS-CODE unable to find a main method?

Time:11-26

Guys why is VSCODE unable to find main method when it's already there ?

static void sayHello(String name) {
    System.out.println("Hello"   name);
}

public static void main(Strings[ ]args) {
    sayHello("David");
}

A screenshot

I also tried adding a class but.. Didn't work.

second attempt

CodePudding user response:

The thing with java is that you want every thing to be inside a class. and, remember this important bit - "the class name should match the file name." so, that is probably the issue. i.e. - in 1, code isn't inside any class. in second, the class name doesn't match the file name. if you are new to java, I suggest going through w3's java tutorial this is a text based tutorial so, you can keep your own pace or even speed run through it [do try out every thing they teach by yourself too]. I seriously recommend it since I started there and almost finished it within 1-2 weeks without any help.

CodePudding user response:

S in String in public static void main(String[] args) { Should be in Uppercase.Error

CodePudding user response:

  1. Your java file's name doesn't match the class name. Please rename your .java file as hello.java.
  2. Pay attention to the args in main method should be String[], while it's Strings[] in your screenshot.
  3. The white dot after your .java file means your file wasn't saved. Save it before running.

hello.java:

public class hello{
    public static void main(String[] args) {
        System.out.println("hello world");
    }
}

Please correct the above things, after Java: Clean Java Language Server Workspace from command Palette the run again.

  • Related