简体   繁体   English

在类中找不到 main(String[]) 方法

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

I am getting the following error :我收到以下错误:

can't find main(String[]) method in class在类中找不到 main(String[]) 方法

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 by java version 12.x 说它是什么原因,但它在onlinegdb.com , GeekforGeek-IDE 等在线编译器中编译得很好,但不能离线编译在java中,我用notepad++编程,直接在命令窗口运行

can't find main(String[]) method in class:Coor在类中找不到 main(String[]) 方法:Coor

Java is looking for the main method in the classname.java file you want to compile. Java 正在寻找您要编译的classname.java文件中的main方法。 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 .我的建议是,您的文件未命名为TcsDigital.java ,因此 java 将采用它找到的第一个 java 类,即Coor Since Coor does not contain a main method an error occurs.由于Coor不包含main方法,因此会发生错误。 You should try renaming your file to TcsDigital.java .您应该尝试将文件重命名为TcsDigital.java

I recommend to write every java class in a sepreate file and to import them as needed.我建议将每个 java 类编写在一个单独的文件中,并根据需要导入它们。 An additional Main class containing only the main method is an option, too.仅包含main方法的附加Main类也是一个选项。

我不知道原因,但是如果我们按照它执行的顺序将公共类排序得更高,而不会出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM