Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in understanding how JVM triggers Main method

 
shiva murugan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[].
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21214
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21149
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. You could use the same reflections logic that Ernest just described.
 
shiva murugan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest, now i understand the concept here.

thanks all who helped me in cracking down this doubt.

Regards
Siva
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic