CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2013
    Posts
    13

    Java class without main method?

    Is it possible to have a class with no main method in Java and still working perfectly??

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java class without main method?

    You need a main() method if you want to start the class with the java command:
    java TheClass
    You don't need a main() method if another class creates an instance of that class:
    TheClass aref = new TheClass();
    also an applet does not use a main() method.

    What does "work perfectly" mean?
    Norm

  3. #3
    Join Date
    Oct 2013
    Posts
    13

    Re: Java class without main method?

    Thank you Norm for your valuable reply, but if main method is required to start a program in Java then my problem is with this code:

    class WithoutMain
    {
    static {
    System.out.println("Hello Java");
    System.exit(0);
    }
    }

    It is working without any error or warning on dos. But eclipse is not able to run it. If this code is working in command prompt, why is it not so with eclipse?

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java class without main method?

    Sorry, I don't know anything about the IDE.

    I never use static blocks so I forgot about them. I always use a main() method in any class I want to execute with the java command.
    Norm

  5. #5
    Join Date
    Oct 2013
    Posts
    13

    Re: Java class without main method?

    Thank you Norm for your replies, I really appreciate it.

  6. #6
    Join Date
    Nov 2013
    Posts
    2

    Re: Java class without main method?

    Blocks are two types one is static and non-static,
    when u use static blocks inside the class till Jdk 1.6 version it will execute with out main(), but from jdk 1.7 version it wont execute. all execution starts from main(). but always static block will execute first.

  7. #7
    Join Date
    Oct 2013
    Posts
    13

    Re: Java class without main method?

    Quote Originally Posted by DineshPazhaniyandi View Post
    Blocks are two types one is static and non-static,
    when u use static blocks inside the class till Jdk 1.6 version it will execute with out main(), but from jdk 1.7 version it wont execute. all execution starts from main(). but always static block will execute first.
    But i am using jdk 1.7 and what do you mean by
    static block will execute first
    What do you want to say? Will it be executed or not?

    I am running the above code successfully but eclipse is not able to run it.

  8. #8
    Join Date
    Nov 2013
    Posts
    2

    Re: Java class without main method?

    first you check jdk version, if jdk 1.7 you are using.. It wont execute.
    first , I executed this program with cmd,
    This is the result
    D:\>javac WithoutMain.java

    D:\>java WithoutMain
    Error: Main method not found in class WithoutMain, please define the main method
    as:
    public static void main(String[] args)
    .............................................................................................................................
    With eclipse:

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



    if you are using jdk version below 1.7, then static block will execute with out main().
    ..................................................................................................................................

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured