简体   繁体   中英

“Main method not found in class…” but it is there ?? so why the error?

I'm hoping someone can help me here. I'm getting the error:

Main method not found in class Pythag , please define the main method as:

  public static void main(String[] args) 

But as you can see in the code below it is there? So why the error? This seems to happen a lot to my classes in Eclipse Java EE IDE.

public class Pythag {

    // All Java applications have a main() method.
    // This one does a simple Pythagorean calculation.
    public static void main(String[] args) {

        // Declare fields and initialise values(input).
        int firstNum = 17;
        int secondNum = 6;
        int answer;

        // Do the calculation (process).
        // 
        answer = (firstNum * firstNum) + (secondNum * secondNum);

        // Display the results (output).
        // 
        System.out.println("The square of the hypotenuse is "
                           + "equal to the sum of the squares "
                           + "of the other two sides.");
        System.out.println("For example...");
        System.out.println(answer + " = " + firstNum + " squared + "
                           + secondNum + " squared.");

    }
}

I just tried VALIDATING my code and it then ran fine? Not sure what the problem was or why it wouldn't work but validating the code seemed to sort it out so, there we go?

Assuming this class is defined in a file called Pythag.java it can be compiled and run like this:

$ javac Pythag.java 
$ java Pythag
The square of the hypotenuse is equal to the sum of the squares of the other two sides.
For example...
325 = 17 squared + 6 squared.

This output was generated by copying your code sample directly into a file called Pythag.java with no edits.

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