2

I need to run a method code for GCD. My java file is called "GCD.java" and the public class is called "GCD." Yet I keep getting the message "Class GCD does not have a main method" even though I have no red explanation point circles in any of my lines. I can run the code without the method code (i.e. public static void main(String[] args)), but I need to run the code with a method. Thanks.

==========================

import java.util.Scanner;

    public class GCD
    {

        public static int getDivisor(int x, int y)
        {


        System.out.println("Greatest Common Divisor Finder");
        System.out.println();

        String choice = "y";
        Scanner sc = new Scanner(System.in);
        while (choice.equalsIgnoreCase("y"))
        {

            System.out.print("Enter first number: ");
            x = sc.nextInt();
            System.out.print("Enter second number: ");
            y = sc.nextInt();

            int secondNumber = 0;
            int firstNumber = 0;
            int Greatestcommondivisionfinder = 0;

            // x = first,  y = second
            if (x > y)
                {
                    do
        {
                        x -= y;
                        }
             while (x > y);
        do
        {
                        y -= x;
                        }
    while (y > 0);
            System.out.println("Greatest Common Divisor: " + x);
        }

            else if (y > x)
        {
        do
                        {
            y -= x;
                        }
    while(y > x);
        do
                        {
            x -= y;
                        }
    while (x > 0);
            System.out.println("Greatest Common Divisor: " + y);
        }
             else
                    {
                    int subtract;
                    do
                    {
                    subtract = (int)y - (int)x;
                    }

            while(y > x);
            int gcd;
            gcd = (int)x - subtract;
                    }


            System.out.println();
            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();
                }
            return 0;
          }
    }
1
  • This is kinda off-topic, but I thought I'd let you know this... In 1998, a smart guy created a search engine, a revolutionary thing. It grew, it became mature and today it's one of the most-visited web sites. You can just enter any combination of keywords (such as "java class does not have a main method") and it'll show you a list of related pages in no time! For your interest, that site is called Google.
    – Tobias
    Mar 16, 2014 at 17:56

3 Answers 3

7

It's entirely valid for a class not to have a main method - or for it to have a main method which isn't declared as public static void main(String[] args).

However, in order to treat a class as the entry point for a Java application, it needs that method, with that signature (although the parameter name can vary).

So basically, you've got a class which is fine in itself, but you can't launch on its own. You could create a separate class, e.g.

public class GcdLauncher {
    public static void main(String[] args) {
        GCD.getDivisor(0, 0); // Parameters are ignored anyway...
    }
}

Then after compilation you could run:

java GcdLauncher

Or you could add a public static void main(String[] args) method to your GCD class.

I would strongly advise you to change your getDivisor method not to have parameters though - you're not actually using them anyway...

0
2

Yes, as your Eclipse correctly says, you don't have main method in your GCD.java file. In-order run this class independently, you need to have main method. Otherwise you can only create Object of this class and call from other class.

1

If your class is to be used as a main program, it has to implement an

public static void main(String[] args))

Method from where you can call your GCD method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.