File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Problem in understanding how JVM triggers Main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem in understanding how JVM triggers Main method" Watch "Problem in understanding how JVM triggers Main method" New topic
Author

Problem in understanding how JVM triggers Main method

shiva murugan
Greenhorn

Joined: Jan 08, 2010
Posts: 17
Hi All,

I might sound stupid, but i have a basic doubt with a plain standalone JAVA program.

consider the following Snippet

public class test1 {


public static void main(Object[] args) {
// TODO Auto-generated method stub

}

}

on executing the above code, i got a fatal error, with this exception
Exception in thread "main" java.lang.NoSuchMethodError: /test1.main([Ljava/lang/String;)V

My doubt is, i am expecting this program to execute properly, since String is a subclass of Object class, but that is not happening.

Is my interpretation is wrong by any chance

Regards
Siva
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3168
    
  10
shiva murugan wrote:My doubt is, i am expecting this program to execute properly, since String is a subclass of Object class, but that is not happening.


You're correct that String is a subclass of Object, but that is irrelevant here as your parameter types are not String or Object. They are String[] and Object[] which is completely different. A String[] is not a subclass of Object[].


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

But it is a subtype. But that's irrelevant here. The issue is that the JVM requires the following:
- there must be a method called main
- which is public
- and static
- and returns void
- and has exactly one parameter, of type String[] - anything else, including Object[], is not allowed


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39


Joanne Neal wrote:
You're correct that String is a subclass of Object, but that is irrelevant here as your parameter types are not String or Object. They are String[] and Object[] which is completely different. A String[] is not a subclass of Object[].


Rob Prime wrote:But it is a subtype. But that's irrelevant here.



I think it depends on how it is defined. If you mean that a String[] is a subtype of Object[] because the instanceof operator return true, then yes, a string array is a subclass of an object array. If you mean the structure that is returned from reflection, then I would lean towards "no" -- as reflection seems to report that all arrays are directly subclassed from the Object class -- or at least, the last time that I checked.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

@henry: yes, that's why I think Rob chose his words carefully. You can write code as if String[] is a subclass of Object[], even if it is not in reality.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Sub typing is broader than sub classing. ArrayList is a sub type of List but it isn't a sub class. String[] is a sub type of Object[] but it isn't a sub class. Vice versa, if a class X is a sub class of class Y then X is also a sub type of Y. instanceof uses sub typing.

Check out the JLS, section 4.10 (note that the "iff" in section 4.10.3 stands for "if and only if").
shiva murugan
Greenhorn

Joined: Jan 08, 2010
Posts: 17
I do not understand the fact here, you guys state that since String Array is not a sub type of Object Array, we get this problem.

if this is the scenario, i believe even this snippet should not work, i should get an error

public class Test {
public static void main(String... a) {
method(new String[1]);
}

static void method(Object[] mnp){
System.out.println("Entered Object Method");
}
}

interestingly it executes and displays the output.

only when JVM tries to trigger main method it get stuck with this problem, in all other scenario, it is working fine.

Regards
Siva
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

shiva murugan wrote:I do not understand the fact here, you guys state that since String Array is not a sub type of Object Array, we get this problem.


String[] is a subtype of Object[] -- no one has said otherwise. But that doesn't matter at all. The Java launcher is looking for a public static void method named "main" that takes an array of String as an argument (and "String..." is ultimately just another way of writing "String[]"). If such a method doesn't exist, then the program won't run. There's nothing any deeper or more meaningful than that -- it's just the rule. It has nothing to do with inheritance or subtypes or anything like that!
shiva murugan
Greenhorn

Joined: Jan 08, 2010
Posts: 17
I think you misinterpreted the snippet in my previous post, i mean to say, i have a user defined method with Object array as an argument, and triggered that method using a String array argument from my main method. since this was working, i am expecting when a JVM tries to call main method with String array argument, it should obviously land inside the main method with object array as argument.

Am i missing any rule of JAVA

Regards
Siva
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

shiva murugan wrote:Am i missing any rule of JAVA

Yes. As said before, the argument type for the main method must be String[] or String... (with varargs). Anything else, even Object[] and Object, will not be called by the JVM. Broadening rules simply do not apply as they do with regular method calls. Period.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

shiva murugan wrote:I think you misinterpreted the snippet in my previous post,


And I think you misinterpreted my answer

OK, I will give you a more technical answer: the Java program launcher uses the Java Reflection API to locate the method to call. It asks for a method with the signature

main([Ljava.lang.String;)V

and if that method is not found, your program won't run. You are creating main(Object[]), which would have the signature

main([Ljava.lang.Object;)V

and therefore the requested method is not found, and the program is not run. Inheritance does not come into play.
shiva murugan
Greenhorn

Joined: Jan 08, 2010
Posts: 17
ok, let me summarize my understanding please correct me if i am wrong.

Backbone of OOPS concept inheritance will not be applicable for JVM when it tries to call main method.
even though main method is also a normal method like other methods in java ,which could be overrided or overloaded, will not be applicable for broadening rule.

if the above said information is true, do any of you guys have any idea, how to implement the same in our program, that is disabling broadening or inheritance rule in java.

I did some googling on this, but it turns to be negative.

Regards
Siva
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18116
    
    8

Sure. You could use the same reflections logic that Ernest just described.
shiva murugan
Greenhorn

Joined: Jan 08, 2010
Posts: 17
Thanks Ernest, now i understand the concept here.

thanks all who helped me in cracking down this doubt.

Regards
Siva
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in understanding how JVM triggers Main method
 
Similar Threads
doubt
overloading doubt
constructor doubt
Static keyword Doubt
Doubt on garbage collection?