-2

When I run my code, I receive this error message:

Error: Main method not found in class "Class name", please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

My code:

public static void main(String[] args){
    public void printPhoto(int width,int height, boolean inColor){
        System.out.println("Width = " + width +  " cm" );
        System.out.println("Height = " + height + " cm");
        if(inColor){
            System.out.println("Print is Full color.");
        } else {
            System.out.println("Print is black and white.");
        }
        printPhoto(10,20,false);
    }
}
MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
World's Smaile
  • 9
  • 1
  • 1
  • 3
  • isn't the error clear enough ? BTW. You need to show us the code for the main method, if indeed there is one. Make sure it is `public static void main(String args[])` – dumbPotato21 May 20 '17 at 13:36
  • Then create a `public static void main(String[] args)` in your class? – QBrute May 20 '17 at 13:37
  • World's Smaile did not ask the question in correct context. Its a problem with Jetbrains idea IDE. I am also having the same problem. I am a seasoned programmer and my class and main method are all correct but when run from inside the idea community edition, it is showing this error. – Nitiraj Dec 19 '17 at 02:52

2 Answers2

2

To start a java program you need the main method which not define in your code you can make it like this :

public class Test {

    public void printPhoto(int width, int height, boolean inColor) {
        System.out.println("Width = " + width + " cm");
        System.out.println("Height = " + height + " cm");
        if (inColor) {
            System.out.println("Print is Full color.");
        } else {
            System.out.println("Print is black and white.");
        }
        // printPhoto(10, 20, false); // Avoid a Stack Overflow due the recursive call
    }

    //main class
    public static void main(String[] args) {
        Test tst = new Test();//create a new instance of your class
        tst.printPhoto(0, 0, true);//call your method with some values        
    }

}
Graham
  • 7,431
  • 18
  • 59
  • 84
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

As already @YCF_L posted, you need the main method.

First, make sure you remove:

printPhoto(10,20,false);

to avoid a Stack Overflow due the recursive call.

If you need to create a new instance and directly use your method, you can do the following:

public class Test {

    public Test printPhoto(int width, int height, boolean inColor) {
        System.out.println("Width = " + width + " cm");
        System.out.println("Height = " + height + " cm");
        if (inColor) {
            System.out.println("Print is Full color.");
        } else {
            System.out.println("Print is black and white.");
        }
        return this;
    }

    public static void main(String[] args) {
        Test testObj = new Test().printPhoto(10, 20, false); //Creates a new instance of your class Test and calls the method printPhoto
    }

}

Alternatively, you could also make the method static, but it's not always the best procedure, it depends on how you are going to use it.

public class Test {

    public static void printPhoto(int width, int height, boolean inColor) {
        System.out.println("Width = " + width + " cm");
        System.out.println("Height = " + height + " cm");
        if (inColor) {
            System.out.println("Print is Full color.");
        } else {
            System.out.println("Print is black and white.");
        }
    }

    public static void main(String[] args) {
        printPhoto(10, 20, false);
    }

}
Graham
  • 7,431
  • 18
  • 59
  • 84
xMarcoGP
  • 5
  • 7