• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JVM thinks that there is no main() method, but there is

 
Dotan Cohen
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Googling the error message it looks like there is no main() method in cohenTest. However, there clearly is. I must be missing something obvious, what might that be?

Thanks!
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sould be
 
Dotan Cohen
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
False alarm, sorry, as soon as I added the "String[] args" parameters definition the application works. In C one can eliminate this, apparently not so in Java.

Thanks!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the correct signiature is



The parameter list is part of the signature. The JVM is specifically looking for that one.
 
Dotan Cohen
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hebert Coelho wrote:It sould be


Thanks, Hebert, you are 100% correct!
 
Dotan Cohen
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:the correct signiature is



The parameter list is part of the signature. The JVM is specifically looking for that one.


Thanks, Fred! That was in fact the issue!
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C and Java™ are very different languages. You should forget entirely how you did it in C.
 
Dotan Cohen
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:C and Java™ are very different languages. You should forget entirely how you did it in C.


Thanks. As any competition driving instructor will tell you: relearning a skill is much harder than learning from scratch! Habits get very ingrained.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and we see some programming habits round here
 
Mohana Rao Sv
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@TS do you know why we should use only this signature public static void main(String[] args) that is the point here you have to understand.
 
Dotan Cohen
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohana Rao wrote:@TS do you know why we should use only this signature public static void main(String[] args) that is the point here you have to understand.


Why is that, exactly? In C, if the application does not accept cli arguments then there is no need to declare main() parameters. Furthermore, the dev is free to use any parameters that he sees fit. Why is main() so restricted in Java? Does the JVM look at the whole signature, and not just at the method/function name (specifically referring to main())? I'm tempted to try to overload main() this evening!
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dotan Cohen wrote:Does the JVM look at the whole signature, and not just at the method/function name (specifically referring to main())? I'm tempted to try to overload main() this evening!

Yes - the full signature defines the method. You can overload main() just like any other method. Everything will be fine, but only one of them will be called by the JVM.

I suppose it could have been designed to allow other signatures...but why bother?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would only make the startup slower. Instead of checking for one method, the JVM would need to check all methods, filter out any that aren't static or public, aren't called "main" and don't have void as the return type. Now it simply looks for one single method, with one fixed signature.
 
Mohana Rao Sv
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@TS Try to explore why main method is public and why static why it's having void return type and args strings[]. Then you can enjoy the essence of java.
 
Dotan Cohen
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:It would only make the startup slower. Instead of checking for one method, the JVM would need to check all methods, filter out any that aren't static or public, aren't called "main" and don't have void as the return type. Now it simply looks for one single method, with one fixed signature.


So the JVM is just grepping for the string "public static void main(String[] args)" disregarding whitespace? It is not parsing each modifier and keyword?

Rather, I expect that all the modifier keywords are being parsed and the JVM is already doing all the work that you mention. This is my guess, I have not looked at the HotSpot code.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dotan Cohen wrote:So the JVM is just grepping for the string "public static void main(String[] args)" disregarding whitespace? It is not parsing each modifier and keyword?

Of course not. The JVM receives compiled byte code, not source code, so the idea of it doing anything like that doesn't make sense.
Rather, I expect that all the modifier keywords are being parsed and the JVM is already doing all the work that you mention. This is my guess, I have not looked at the HotSpot code.

Well, no. The compiler does all of that parsing. The JVM simply takes the compiled class and looks to see if it has a method with the specified signature.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul is correct. You can swap the public and static modifiers around, add whitespace, rename the parameter and move the [] to after the parameter name. All that matters is that:
- the method is public
- the method is static
- the method is called main
- the method has one parameter of type String[]*

* Actually, since Java 5.0 you can also use varargs as well, but only String[] and String... are allowed.
 
Dotan Cohen
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Dotan Cohen wrote:So the JVM is just grepping for the string "public static void main(String[] args)" disregarding whitespace? It is not parsing each modifier and keyword?

Of course not. The JVM receives compiled byte code, not source code, so the idea of it doing anything like that doesn't make sense.

I've got screenfuls of PHP open in another window, I was in uncompiled scripting mindset apparently! But the argument remains, surely the compiler makes sense of the modifiers and doesn't simply grep for a known string. So why is there a requirement for this exact signature? I can think of situations in which I want the code to accept only a single int as a cli parameter, so why can't I code that into the method? Or to omit the unnecessary parameters altogether? In the name of consistency then shouldn't all other methods be overloaded to accept any and all arguments thrown at them?
 
Dotan Cohen
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Paul is correct. You can swap the public and static modifiers around, add whitespace, rename the parameter and move the [] to after the parameter name. All that matters is that:
- the method is public
- the method is static
- the method is called main
- the method has one parameter of type String[]*

* Actually, since Java 5.0 you can also use varargs as well, but only String[] and String... are allowed.


But the question remains, why the requirement for accepting any and all arguments thrown at it? No other method has this compulsion.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you mean arguments to the JVM itself, that's because the JVM puts them into the String[]. This is the one single argument you get, and it's equivalent to C's char** argv. C just adds the int argc because it's otherwise impossible to determine the end of the argv. In Java you can get that from args.length.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dotan Cohen wrote:But the question remains, why the requirement for accepting any and all arguments thrown at it? No other method has this compulsion.

No other method is used as the starting point to the application, either, so I don't see there's any need for consistency.

If multiple main method signatures were used by the JVM, then it would be poorly defined which one would be called if you implemented several of them. At the very least it would lead to confusion - having a different method run just because the command-line arguments were different seems highly counter-intuitive to me. And if you then decided to enforce a restriction on overloading the main method to avoid this, then that seems a greater inconsistency. As it stands the rules for compiling the main method are the same as any other method, and it's only the JVM that treats it as special.
 
Dotan Cohen
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Dotan Cohen wrote:But the question remains, why the requirement for accepting any and all arguments thrown at it? No other method has this compulsion.

No other method is used as the starting point to the application, either, so I don't see there's any need for consistency.


No other method is used to square ints, so I don't see there's any need for accepting string values. By your argument all methods should simply take an array of strings as the "parameters" and let the method body sort out how to cast them and "overload".

If multiple main method signatures were used by the JVM, then it would be poorly defined which one would be called if you implemented several of them. At the very least it would lead to confusion - having a different method run just because the command-line arguments were different seems highly counter-intuitive to me. And if you then decided to enforce a restriction on overloading the main method to avoid this, then that seems a greater inconsistency. As it stands the rules for compiling the main method are the same as any other method, and it's only the JVM that treats it as special.


I would expect that having a different method run because the cli arguments were different would be as natural as any other form of overloading methods.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dotan Cohen wrote:By your argument all methods should simply take an array of strings as the "parameters" and let the method body sort out how to cast them and "overload".


You are turning Matthew's argument about the specific method called by the JVM to start a Java program running into an argument about all possible methods of all possible classes. This of course is an absurd characterization of what he posted.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No other method is used to square ints, so I don't see there's any need for accepting string values. By your argument all methods should simply take an array of strings as the "parameters" and let the method body sort out how to cast them and "overload".


Not really. The main() method is special. It's the only method that starts the program, so has a special status in the JVM. So generalising from it to other methods is unlikely to be helpful. Given that what the JVM receives is an arbitrary list of strings as command-line arguments, then a String array seems a sensible type to represent that with.

Dotan Cohen wrote:I would expect that having a different method run because the cli arguments were different would be as natural as any other form of overloading methods.


Really? That sounds like a source of difficult-to-find bugs to me. But it's a valid opinion. Let's just say that I disagree, as did the designers of the Java language.
 
Dotan Cohen
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is interesting the hear the different viewpoints about the restrictions on the main() signature, however I do think that the topic has gone on a bit too far and rereading one of my own posts it looks like I was even a bit aggressive in my wording. Therefore I will simply thank everyone for contributing their views on the matter. I am certainly too new to Java to have any informed opinion on it, but the opinions of those who do have experience are very important for me to hear.

Have a terrific weekend to all!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic