File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block without main().

 
Ritesh raushan
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5116
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/specs/jvms/se5.0/html/Concepts.doc.html#19042
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all....
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic