Error : Main method not found in java class

1.6k views Asked by At

I am new to concepts of java. while preparing my first program of classes with objects i encountered a problem. here is the code and the error..please resolve.. PROGRAM:

class Fact
{
    private int i;
    private int n;
    private int fact;

    public Fact()
        { fact=1;
          i=1;
        }
    public Fact( int x)
        { n=x; }
    public void getAnswer()
        {
            while(i<=n)
            {fact=fact*i;
                i++;}
            System.out.println(fact);
        }
}
class FactMain
{
    public static void main(String dt[])
    {
    Fact obj= new Fact(6);
    obj.getAnswer();
    }
}

ERROR:

Main method not found in class Fact, please define the main method as:
public static void main(String[] args)
6

There are 6 answers

2
mazhar islam On BEST ANSWER

Rename the class file name Fact.java to FactMain.java.

private int fact;

public Fact()
    { fact=1;
      i=1;
    }
public Fact( int x)
    { n=x; }

Note, your default constructor set fact but constructor Fact( int x) set n. Hence fact is 0. So your output is 0 too.

Solution:

public Fact(int x) {
    fact = 1;
    i = 1;
    n = x;
}

Or,

public Fact(int x) {
    this(); // default constructor
    n = x;
}

Here is the complete solution:

Create a single class file named FactMain.java, then paste the following code:

class Fact {
    private int i;
    private int n;
    private int fact;

    public Fact() {
        fact = 1;
        i = 1;
    }

    public Fact(int x) {
        this();
        n = x;
    }

    public void getAnswer() {
        while (i <= n) {
            fact = fact * i;
            i++;
        }
        System.out.println(fact);
    }
}

class FactMain {
    public static void main(String[] dt) {
        Fact obj = new Fact(6);
        obj.getAnswer();
    }
}
1
Codebender On

You have saved your file as Fact.java. So java is trying to find the main class in Fact. Save your file as FactMain.java It should work.

3
Abhijeet Dhumal On

Your main method is in FactMain.java, but you are saving a file as Fact.java.

You will need to save the file as FactMain.java as JVM expects main to be in the same class as the name of .java file.

0
Sarfaraz Khan On

You have defined your main class in FactMain and most probably after compilation while running you're trying to execute

java Fact

And hence you got the error because there is no main method in Fact class. Once you compile the .java file you will get two class files Fact.class and FactMain.class so you should execute

java FactMain

6
MadProgrammer On

Move the FactMain class to FactMain.java

FactMain.java

public class FactMain
{
    public static void main(String dt[])
    {
        Fact obj= new Fact(6);
        obj.getAnswer();
    }
}

Allow the Fact class to remain in the Fact.java file

Fact.java

public class Fact {

    private int i;
    private int n;
    private int fact;

    public Fact() {
        fact = 1;
        i = 1;
    }

    public Fact(int x) {
        this();
        n = x;
    }

    public void getAnswer() {
        while (i <= n) {
            fact = fact * i;
            i++;
        }
        System.out.println(fact);
    }
}

Compile the classes...

javac {package path}\FactMain.java

Run the main class

java {package path}.FactMain
1
Hiren On

just change your Parameterized constructor to this

public Fact(int x) {
    fact = 1;
    i = 1;
    n = x;
}

because you declare factorial in default constructor and you are not calling it. So, 0 is assigned to factorial and then you r trying to multiply it. Which makes no sense.