简体   繁体   English

Java拒绝public static void main方法,请求public static void main方法

[英]Java rejects public static void main method, requests public static void main method

I am trying to follow a piece of code to open and read a text file. 我正在尝试遵循一段代码来打开和读取文本文件。 To do this I have a package called readText. 为此,我有一个名为readText的包。 Within I build a class readLocalFile to open and read the file, and a main method to call it. 在其中,我建立了一个类readLocalFile来打开和读取文件,以及一个调用它的主要方法。 Below are these two classes. 以下是这两个类。

public class readFileLocal {
    private String path;

    public readFileLocal(String file_path){
        path = file_path;
    }

        int readLines() throws IOException{
            FileReader file_to_read = new FileReader(path);
            BufferedReader lines = new BufferedReader (file_to_read);

            int numberOfLines = 0;
            while(lines.readLine()!= null) { 
                numberOfLines ++; 
            }  

            lines.close();
            return numberOfLines;
        }

        public String[] openFile() throws IOException{  
            FileReader freader = new FileReader (path); 
            BufferedReader textReader = new BufferedReader (freader); 

            int numberOfLines = readLines();   
            String[] textData = new String[numberOfLines]; 

            int i;  /* put all the lines of text from the file to the array*/
            for (i=0; i<numberOfLines; i++){
                textData[i] = textReader.readLine(); 
        }

        textReader.close();
        return textData;
    }
}

Then I have a main class to call it. 然后我有一个主要班级来称呼它。 The code is below: 代码如下:

public class fileData {

    public static void main(String[] args) throws IOException{

        String file_name = "F:/Testfile.exl";

        try{
            readFileLocal file = new readFileLocal(file_name);  
            String[] arylines = file.openFile();

            int i;
            for (i=0; i<arylines.length; i++){
                System.out.println(arylines[i]);
            }
        }
        catch(IOException e) { 
            System.out.println(e.getMessage()); 
        }
    }
}

When I ran it, Eclipse gave me this error message: 当我运行它时,Eclipse给了我这个错误消息:

Error: Main method not found in class readText.fileData, please define the main method as:public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application 错误:在类readText.fileData中找不到主要方法,请将该主要方法定义为:public static void main(String [] args)或JavaFX应用程序类必须扩展javafx.application.Application

Any idea what went wrong? 知道出了什么问题吗?

You probably should start a new project. 您可能应该开始一个新项目。 Eclipse thinks that you are running a JavaFX program (in which case filedata should extend Application). Eclipse认为您正在运行JavaFX程序(在这种情况下,文件数据应扩展Application)。

Just do a normal build without JavaFX. 只需执行没有JavaFX的常规构建即可。

Probably, you have declared your own String class in the same package. 可能您已经在同一包中声明了自己的String类。 In this case Eclipse doesn't recognize expected java.lang.String in your main method. 在这种情况下,Eclipse在您的main方法中无法识别预期的java.lang.String Edit your main method declaration as 将主方法声明编辑为

public static void main(java.lang.String[] args)

and try to run it. 并尝试运行它。

I loaded your code into my copy of Eclipse and it ran the main method without a problem. 我将您的代码加载到我的Eclipse副本中,它可以正常运行主要方法。 One curious thing, when I saved the file, I got a message about characters being encoded as "Cp1252" encoding rather than the expected UTF-8. 奇怪的是,当我保存文件时,我收到一条消息,提示字符被编码为“ Cp1252”编码,而不是预期的UTF-8。 I cut and paste your code into Eclipse from StackOverflow. 我将代码从StackOverflow剪切并粘贴到Eclipse中。 Maybe this has something to do with the problem? 也许这与问题有关?

在此处输入图片说明

In Eclipse, you can check what is going on by selecting 在Eclipse中,您可以选择

Run > Run Configurations...

The "Main" tab will display what Eclipse thinks is the main method to be invoked, and other tabs will show if there are any arguments being sent in. “ Main”选项卡将显示Eclipse认为是要调用的主要方法,其他选项卡将显示是否有任何参数要发送。

Also, it might be worth running 另外,可能值得运行

Project > Clean

for you project. 为您的项目。

Crazy thing to check: are there any other classes with the same name? 疯狂的事情要检查:是否还有其他同名的类? Do you have more than one class defined on the same class document? 在同一个班级文档上定义了多个班级吗?

Another thing to check, navigate to the class document, "fileData" (really should be FileData, follow conventions because otherwise it adds to everyone's confusion who is trying to help or work with you), and right click. 要检查的另一件事,导航到类文档“ fileData”(实际上应该是FileData,遵循约定,因为否则会加重每个试图帮助您或与您合作的人的困惑),然后单击鼠标右键。 Does the right click give you the following? 右键单击会为您提供以下内容吗?

Run As > 1) Java Application

Or does it show this? 还是表明这一点?

Run As > Run Configurations...

The first case indicates it found the main method, in the second case, no main method was found. 第一种情况表明找到了主要方法,第二种情况没有找到主要方法。

The stuff about JavaFX can be ignored. 关于JavaFX的内容可以忽略。 You aren't running JavaFX according to any code I see displayed, so that issue is moot. 您没有按照我看到的任何代码运行JavaFX,所以这个问题没有解决。

BTW, in your main method, you catch IOException in the try/catch, so there is no need to include "throws IOException" in the main method. 顺便说一句,在您的主要方法中,您在try / catch中捕获了IOException,因此不需要在主要方法中包括“ throws IOException”。

暂无
暂无

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

相关问题 Java - public static void main() - Java - public static void main() Java-使用JUnit测试公共静态void主方法 - Java - test a public static void main method with JUnit 如何使用 JUnit 5 测试 Java 中的 Public Static Void Main 方法? - How to test the Public Static Void Main method in Java using JUnit 5? 如何在public static main中使用public void方法 - How to use public void method in public static main 对main方法的误解/“ public static void main(String [] args)”的观点 - Misunderstanding main method/the point of “public static void main(String[] args)” 覆盖范围不能覆盖public static void main方法 - Coverage can't cover the public static void main method Kotlin 中的 public static void main - public static void main in Kotlin 错误:在类Text中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class Text, please define the main method as: public static void main(String[] args) 错误:在Binary类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class Binary, please define the main method as: public static void main(String[] args) 错误:在TextBook类中找不到主要方法,请将该主要方法定义为:public static void main(String [] args) - Error: Main method not found in class TextBook, please define the main method as: public static void main(String[] args)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM