简体   繁体   中英

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. Within I build a class readLocalFile to open and read the file, and a main method to call it. 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:

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

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).

Just do a normal build without JavaFX.

Probably, you have declared your own String class in the same package. In this case Eclipse doesn't recognize expected java.lang.String in your main method. 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. 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. I cut and paste your code into Eclipse from StackOverflow. Maybe this has something to do with the problem?

在此处输入图片说明

In Eclipse, you can check what is going on by selecting

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.

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. 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. You aren't running JavaFX according to any code I see displayed, so that issue is moot.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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