aspose file tools*
The moose likes Beginning Java and the fly likes 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 » Beginning Java
Bookmark "Main Method" Watch "Main Method" New topic
Author

Main Method

Wayne Styles
Ranch Hand

Joined: Aug 18, 2006
Posts: 45
Hi
I keep getting main method errors. I have copied code and pasted it, but it still wont compile or run, can you see the problems please.
package com.ack.learning;

public interface AnInterface {
// can define constants in interfaces
public static final int MAX_VALUE = 300;

// but really use interfaces for declaring methods
public void aMethod();

public int anotherMethod( String hey ) throws Exception;

public void yetAnotherMethod() throws Exception, ArithmeticException;
}

class ImplementInterfaceClass implements AnInterface {

public void aMethod() {

}

// note that methods are matched on formal parameter types and
// method name.
public int anotherMethod( String yep ) throws Exception {
// note that when you implement an interface, its constants
// are brought into the defining class's scope
if( true ) {
return MAX_VALUE;
}
else {
// however, best to always prefix constants with the defining
// class or interface for readability purposes
return AnInterface.MAX_VALUE;
}
}

/**
* this causes a duplication definition of anotherMethod because
* the method signature only differs by the return type. In Java,
* if two method differ only by the return type they are the same method
* from an inheritance perspective
*
public String anotherMethod(String yep) throws Exception
{
return null;
}
*/

// finally, you can selectively choose which exception to declare
// on the throws clause of the implementing method, here we choose to omit
// Exception
public void yetAnotherMethod() throws ArithmeticException {

}

}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Wayne Styles:
Hi
I keep getting main method errors. I have copied code and pasted it, but it still wont compile or run, can you see the problems please...

Your code compiles fine for me. Could you post the errors you're getting?

Once it does compile... In order to run a program from a command line, you need a "main method" of the form...

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

This method is called when you enter "java SomeClass" at the command line, so whatever you put in the main method body is what actually runs.
[ August 21, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Wayne Styles
Ranch Hand

Joined: Aug 18, 2006
Posts: 45
Hi
java.lang.NoSuchMethodError: main
Exception in thread "main"
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Wayne Styles:
Hi
java.lang.NoSuchMethodError: main
Exception in thread "main"


How are you compiling and running this program? From my experience, if you got this message, then your code DOES compile. However, it cannot run because (as the error says) there is no method named "main". You need to create sucha method with the correct signature as marc describes above. Let us know if you need more help.

Layne


Java API Documentation
The Java Tutorial
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Wayne Styles:
Hi
java.lang.NoSuchMethodError: main
Exception in thread "main"

This looks like it's compiling okay, but when you try to run, it just can't find a main method. See my post above (which I added to while you were responding).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Main Method
 
Similar Threads
Interface and Abstract Classes
Interface
override and overload method
Are static methods are overriden??
programming to an interface