jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes JVM thinks that there is no main() method, but there is Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JVM thinks that there is no main() method, but there is" Watch "JVM thinks that there is no main() method, but there is" New topic
Author

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

Dotan Cohen
Ranch Hand

Joined: Feb 09, 2011
Posts: 40


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

Joined: Jul 14, 2010
Posts: 754

It sould be


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Dotan Cohen
Ranch Hand

Joined: Feb 09, 2011
Posts: 40
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

Joined: Oct 02, 2003
Posts: 11175
    
  16

the correct signiature is



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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Dotan Cohen
Ranch Hand

Joined: Feb 09, 2011
Posts: 40
Hebert Coelho wrote:It sould be


Thanks, Hebert, you are 100% correct!
Dotan Cohen
Ranch Hand

Joined: Feb 09, 2011
Posts: 40
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

Joined: Oct 13, 2005
Posts: 38107
    
  22
C and Java™ are very different languages. You should forget entirely how you did it in C.
Dotan Cohen
Ranch Hand

Joined: Feb 09, 2011
Posts: 40
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

Joined: Oct 13, 2005
Posts: 38107
    
  22
. . . and we see some programming habits round here
Mohana Rao Sv
Ranch Hand

Joined: Aug 01, 2007
Posts: 485

@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.


ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
Dotan Cohen
Ranch Hand

Joined: Feb 09, 2011
Posts: 40
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

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mohana Rao Sv
Ranch Hand

Joined: Aug 01, 2007
Posts: 485

@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

Joined: Feb 09, 2011
Posts: 40
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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

Joined: Feb 09, 2011
Posts: 40
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

Joined: Feb 09, 2011
Posts: 40
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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Feb 09, 2011
Posts: 40
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Feb 09, 2011
Posts: 40
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!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JVM thinks that there is no main() method, but there is
 
Similar Threads
Illegal start of expression
Why doesn't 'final String[][] ....' make it final?????
a strange question about polymorphism
ibm test question
problems with math methods