aspose file tools*
The moose likes Beginning Java and the fly likes static block without main(). Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static block without main()." Watch "static block without main()." New topic
Author

static block without main().

Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
when i run this code it gives error at run time main method not found in Child class.
but static blolck execute at class loading then why he gives this types of error.if i write simply blank body of main() then code executes.


Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

http://docs.oracle.com/javase/specs/jvms/se5.0/html/Concepts.doc.html#19042


luck, db
There are no new questions, but there may be new answers.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Ritesh raushan wrote:when i run this code it gives error at run time main method not found in Child class.
but static blolck execute at class loading then why he gives this types of error.if i write simply blank body of main() then code executes.



looks like you have java 7 installed on your system and,
this trick does not work in java 7.
prerior to java 7. output would be.
20
Exception in thread "main" java.lang.NoSuchMethodError: main

in java 7 the output would be..
Error: Main method not found in class Test, please define the main method as:
public static void main(String[] args)


try to match your output with above outputs and i found that you are getting error of second type.
means you are using java 7.


OCPJP 6 86%
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

did you read the link given by Darryl Burke ? it clearly states that JVM begins execution of a program starting by looking at the main method . if there is no proper main method found( from proper means declared as public static and taking String[] as argument) the JVM will throw NoSuchMethodError
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

gurpeet singh wrote:did you read the link given by Darryl Burke ? it clearly states that JVM begins execution of a program starting by looking at the main method . if there is no proper main method found( from proper means declared as public static and taking String[] as argument) the JVM will throw NoSuchMethodError

Hey Buddy...
Looks like you are ignoring my last post.
In his case JVM is throwing...
Error: Main method not found in class Test, please define the main method as:
public static void main(String[] args)

and not..
Exception in thread "main" java.lang.NoSuchMethodError: main
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

gurpeet singh wrote:did you read the link given by Darryl Burke ? it clearly states that JVM begins execution of a program starting by looking at the main method . if there is no proper main method found( from proper means declared as public static and taking String[] as argument) the JVM will throw NoSuchMethodError


As already stated, prior to Java 7, it executes any static initializers first, if present. Then it gives the exception. And in Java 7, it just gives a different error, without even executing the initializers. Note that DB's link is for a pre-7 JVM.



just prints out, "Hello, World!" and then exits with no exception in Java 6.

Icky, but true.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

thanks for the information guys. guess java 7 is smarter than java 6 !!!. i think now i should listen to that java pop up in notification area to update java version to 7
Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
thanks to all....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
It used to be a party trick, asking how to run an app without a main method, and you can probably find such threads in the “MD” forum. But it doesn’t work any more
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Note that while the link DB provided is for the 2nd edition of the JVM spec (valid for Java SE 5 and 6), the Java SE 7 version of the JVM spec has essentially the same info. Both versions essentially say that the main(String[]) method must be public and static, but neither spec actually says what the behavior is supposed to be if a user violates the spec. That's entirely up to the implementors. Evidently someone at Oracle decided to shut down this particular loophole - but they only shut it down in the implementation. As far as I can tell, the specs remain silent on this issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static block without main().