Eclipse - NoSuchMethodError: main

W

William Colls

I think this is really an Eclipse problem.

When I try to run my program, I get the error message

NoSuchMethodFound: main

The file does in fact contain a main method, declared as public static
void, with no arguments.

I suspect that Eclipse is looking in the wrong place for the method, but
in every place that I can find in the various configurations, the file
that I am running is identified as the file containing containing the
main() method.

Any suggestions as to where further to look will greatfully received.

Thanks for your time.

William.
 
J

Jussi Piitulainen

William said:
I think this is really an Eclipse problem.

When I try to run my program, I get the error message

NoSuchMethodFound: main

The file does in fact contain a main method, declared as public static
void, with no arguments.

Have you tried declaring it correctly?

Either: public static void main(String[] args)
or: public static void main(String... args).

<http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.1.4>
 
M

markspace

I think this is really an Eclipse problem.

When I try to run my program, I get the error message

NoSuchMethodFound: main

The file does in fact contain a main method, declared as public static
void, with no arguments.


No arguments? In Java, the signature must match exactly. The argument
declaration is required.
 
L

Lew

William said:
I think this is really an Eclipse problem.
Nope.

When I try to run my program, I get the error message

NoSuchMethodFound: main

The file does in fact contain a main method, declared as public static
void, with no arguments.

It's a programmer problem.
I suspect that Eclipse is looking in the wrong place for the method, but
in every place that I can find in the various configurations, the file
that I am running is identified as the file containing containing the
main() method.

Any suggestions as to where further to look will greatfully [sic] received.

Jussi and markspace explained the problem.

I will generalize the answer for you:

Do not suspect Eclipse first. Do not suspect Java first. Always suspect the programmer first.
 
E

Eric Sosman

I think this is really an Eclipse problem.

When I try to run my program, I get the error message

NoSuchMethodFound: main

The file does in fact contain a main method, declared as public static
void, with no arguments.

I suspect that Eclipse is looking in the wrong place for the method, but
in every place that I can find in the various configurations, the file
that I am running is identified as the file containing containing the
main() method.

Any suggestions as to where further to look will greatfully received.

Others have explained that "main" must have a parameter list
consisting of exactly one String array. For completeness' sake,
it's *possible* to have a "main" with some other signature; as with
any other method, you can overload "main" as you wish:

public static void main(String[] args) { ... }
public static int main() { return 42; }
public static double main(String justOneArg) {
return Double.parseDouble(justOneArg);
}
...

However, the particular "main" that Java (not just Eclipse) looks
for when starting execution is the one and only "main" that has the
first form above. (You can write "String... args" instead of
"String[] args" if you like, but it's the same thing. Java starts
with the "main" whose parameter list is one String array, however
you happen to express it.)

Only the type of the formal parameter matters; the actual name
is irrelevant. When the arguments aren't used, my own personal
practice is to write

public static void main(String[] unused) { ... }

The only drawback is that code-policing tools sometimes insist
that I write Javadoc comments for the "unused" parameter ;-)
 
A

Arne Vajhøj

Only the type of the formal parameter matters; the actual name
is irrelevant. When the arguments aren't used, my own personal
practice is to write

public static void main(String[] unused) { ... }

The only drawback is that code-policing tools sometimes insist
that I write Javadoc comments for the "unused" parameter ;-)

That could be considered a good opportunity to put in some
funny comments!

Arne
 
B

BGB

Only the type of the formal parameter matters; the actual name
is irrelevant. When the arguments aren't used, my own personal
practice is to write

public static void main(String[] unused) { ... }

The only drawback is that code-policing tools sometimes insist
that I write Javadoc comments for the "unused" parameter ;-)

That could be considered a good opportunity to put in some
funny comments!

yep, and maybe go into some long rant about something almost entirely
irrelevant (hmm... sadly not too far from the truth sometimes...).
 
A

Arne Vajhøj

BGB said:
On 3/21/2012 8:46 PM, Eric Sosman wrote:
Only the type of the formal parameter matters; the actual name
is irrelevant. When the arguments aren't used, my own personal
practice is to write

public static void main(String[] unused) { ... }

The only drawback is that code-policing tools sometimes insist
that I write Javadoc comments for the "unused" parameter ;-)

That could be considered a good opportunity to put in some
funny comments!

yep, and maybe go into some long rant about something almost entirely
irrelevant (hmm... sadly not too far from the truth sometimes...).

How about "This comment is intentionally left empty."?

@param unused What do you think?
@param unused To be used or not to be used that is the question!

:)

Arne
 
L

Lew

Arne said:
@param unused What do you think?
@param unused To be used or not to be used that is the question!

:)

* @param unused as I am to public speaking, I stand before you ...
 
W

William Colls

I think this is really an Eclipse problem.

When I try to run my program, I get the error message

NoSuchMethodFound: main

The file does in fact contain a main method, declared as public static
void, with no arguments.

I suspect that Eclipse is looking in the wrong place for the method, but
in every place that I can find in the various configurations, the file
that I am running is identified as the file containing containing the
main() method.

Any suggestions as to where further to look will greatfully received.

Others have explained that "main" must have a parameter list
consisting of exactly one String array. For completeness' sake,
it's *possible* to have a "main" with some other signature; as with
any other method, you can overload "main" as you wish:

public static void main(String[] args) { ... }
public static int main() { return 42; }
public static double main(String justOneArg) {
return Double.parseDouble(justOneArg);
}
...

However, the particular "main" that Java (not just Eclipse) looks
for when starting execution is the one and only "main" that has the
first form above. (You can write "String... args" instead of
"String[] args" if you like, but it's the same thing. Java starts
with the "main" whose parameter list is one String array, however
you happen to express it.)

Only the type of the formal parameter matters; the actual name
is irrelevant. When the arguments aren't used, my own personal
practice is to write

public static void main(String[] unused) { ... }

The only drawback is that code-policing tools sometimes insist
that I write Javadoc comments for the "unused" parameter ;-)
Thanks to all who replied. I changed the the signature to

public static void(String[] args)

but it made no difference - same problem.

I made the assumption that it was/is an Eclipse configuration issue,
based on the fact that When I first started with Java, I was using the
Netbeans IDE, and the main() worked perfectly well there, but I would
get the NoSuchMethodError if project setup page didn't point to the
correct file.

I have side-stepped the problem by creating a small class that does have
a main method, imports the class I have defined and am building, and
then instansiates an object form it, and invokes the methods I need to
test. Better solution, as it more closely the real world use of the work
I am doing.

Again thanks to all for your helpful responses.

William.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top