Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Main method not found?

  1. #1
    Junior Member
    Join Date
    Sep 2017
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Main method not found?

    I copied this exactly from my book:

    public class Block {
        int a, b, c;
        int volume;
     
        Block(int i, int j, int k){
            a=i;
            b=j;
            c=k;
            volume=a*b*c;        
        }
        boolean sameBlock(Block ob){
            return (ob.a==a)&(ob.b==b)&(ob.c==c);
        }
        boolean SameVol(Block ob){
            return (ob.volume==volume);
        }
    }
     
        class PassOb{
        public static void main(String[] args) {
            Block ob1= new Block(10, 2, 5);
            Block ob2 = new Block(10, 2, 5);
            Block ob3 = new Block(4, 5, 5);
     
            System.out.println("ob1 same dimensions as ob2; " +
                            ob1.sameBlock(ob2));
            System.out.println("ob1 same dimensions as ob3; " +
                            ob1.sameBlock(ob3));
            System.out.println("ob1 same volume as ob3; " +
                                ob1.SameVol(ob3));
     
        }
     
    }

    but when I try to run it by pressing F6 I get an error message that reads:

    Error: Main method not found in class block.Block, please define the main method as:
       public static void main(String[] args)
    or a JavaFX application class must extend javafx.application.Application
    C:\Users\owner\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 9 seconds)
    .


    Now I can get it to run correctly and produce the desired output by pressing Shift+F6, but I'm wondering why I'm getting an error message at all.

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    258
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Main method not found?

    Have you declared the package ?
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Sep 2017
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Main method not found?

    Yes.

    package block;
     
    /**
     *
     * @author owner
     */
    public class Block {
        int a, b, c;
        int volume;
     
        Block(int i, int j, int k){
            a=i;
            b=j;
            c=k;
            volume=a*b*c;        
        }
        boolean sameBlock(Block ob){
            return (ob.a==a)&(ob.b==b)&(ob.c==c);
        }
        boolean SameVol(Block ob){
            return (ob.volume==volume);
        }
    }
     
        class PassOb{
        public static void main(String[] args) {
            Block ob1= new Block(10, 2, 5);
            Block ob2 = new Block(10, 2, 5);
            Block ob3 = new Block(4, 5, 5);
     
            System.out.println("ob1 same dimensions as ob2; " +
                            ob1.sameBlock(ob2));
            System.out.println("ob1 same dimensions as ob3; " +
                            ob1.sameBlock(ob3));
            System.out.println("ob1 same volume as ob3; " +
                                ob1.SameVol(ob3));
     
        }
     
    }

    Packages are declared automatically in NetBeans. I created a project in NetBeans called Block and then copied the code into it. There was no package in the book.
    Last edited by Michael3; October 1st, 2017 at 09:02 PM.

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    258
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Main method not found?

    I run your code using IDE, it works. Here the output

    ob1 same dimensions as ob2; true
    ob1 same dimensions as ob3; false
    ob1 same volume as ob3; true
    Whatever you are, be a good one

  5. #5
    Junior Member
    Join Date
    Sep 2017
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Main method not found?

    Yes, that's the right output. It comes out correctly when I press Shift+F6. I was just wondering why it gives me the error message about not having a main method when I try to run it using only F6. I have other saved projects that run correctly when I only press F6.

  6. #6
    Junior Member
    Join Date
    Oct 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Main method not found?

    r u running PassOb class or Block class

    --- Update ---

    java polymorphism | method overloading | mehtod overriding

Similar Threads

  1. Failed to compile, Main Method Not found, assistance please.
    By rickyjoepr@gmail.com in forum Object Oriented Programming
    Replies: 3
    Last Post: September 1st, 2014, 07:17 AM
  2. <No main class found>??
    By Texavantz in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 7th, 2014, 04:19 PM
  3. Error: Main method not found! Please help!
    By adnan.alvee in forum What's Wrong With My Code?
    Replies: 12
    Last Post: March 11th, 2013, 07:03 PM
  4. Why am I getting a <No Main Classes Found> error message?? Help please!!
    By Rick Sebastian in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 9th, 2013, 06:04 PM
  5. Main Class Not Found Problem
    By shadow in forum Java Theory & Questions
    Replies: 3
    Last Post: September 29th, 2009, 09:42 AM