Home » Java » java – can't find main(String[]) method in class-Exceptionshub

java – can't find main(String[]) method in class-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am getting the following error :

can’t find main(String[]) method in class

import java.util.*;

class Coor {
    int x, y, w;
    Coor(int x, int y, int w) {
        this.x = x;
        this.y = y;
        this.w = w;
    }
}

class Xc {
    int c = 0;
    int d = 10;
    Xc(int c, int d) {
        this.c = c;
        this.d = d;
    }

}

public class TcsDigital {
    int n = 4;
    boolean visited = false;
    boolean[][] varray = new boolean[n][n];
    //int array[][]=new int[n][n];
    int array[][] = {
        {
            1,
            8,
            21,
            7
        },
        {
            19,
            17,
            10,
            20
        },
        {
            2,
            18,
            23,
            22
        },
        {
            14,
            25,
            4,
            13
        }
    };


    public boolean check(int r, int c) {
        if (r >= 0 && r < n && c >= 0 && c < n && varray[r][c] == false)
            return true;
        return false;
    }

    public void fun(int[][] a, int r, int c, int w) {
        if (r == n - 1 && c == n - 1) {
            System.out.println("Reached");
            return;
        }

        varray[r][c] = true;

        if (check(r + 1, c) == true) {

        }
        if (check(r - 1, c) == true) {

        }
        if (check(r, c + 1) == true) {

        }
        if (check(r, c - 1) == true) {

        }
        varray[r][c] = false;
    }
    public static void main(String[] args) {
        TcsDigital t = new TcsDigital();
        t.fun(t.array, 0, 0, t.array[0][0]);
        //Stack <Coor>stack=new Stack<Coor>();
        Stack < Xc > stacks = new Stack < Xc > ();

    }
}

An error occurs can’t find main(String[]) method in class
says by java version 12.x
what reason it says like that, but it compiles well in online compilers like onlinegdb.com, GeekforGeek- IDE, but not compile offline in java, I programmed with notepad++ and run directly at the command window

can’t find main(String[]) method in class:Coor

How to&Answers:

Java is looking for the main method in the classname.java file you want to compile. The name of the file has to correspond with the class found in it.

My suggestion is, that your file is not named TcsDigital.java, therefore java is taking the first java class it finds, which is Coor. Since Coor does not contain a main method an error occurs.
You should try renaming your file to TcsDigital.java.

I recommend to write every java class in a sepreate file and to import them as needed. An additional Main class containing only the main method is an option, too.