Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Exception in thread "main" java.lang.NoSuchMethodError: main

843785Aug 28 2008 — edited Aug 28 2008
Hi All,

I'm learning Java and am running into a problem I believe is common and in this case related to the import statement. I just can not quite figure out what is causing the error in the subject of this post. Here is the code I'm trying to compile. It compiles fine. Here is how I'm attempting to run it.

e:\java_tutorial>java -cp . MagicHat
Then I get
Exception in thread "main" java.lang.NoSuchMethodError: main

Code:

import java.util.Random; //Import Random Class

public class MagicHat
{
static int maxRabbits = 5; //Maximum Rabbits in a hat
static Random select = new Random(); //RAdnom number generator

//Constructor for a hat
public MagicHat(final String hatName)
{
this.hatName = hatName;
rabbits = new Rabbit[1+select.nextInt(maxRabbits)]; // Random rabbits

for(int i = 0; i < rabbits.length; i++)
rabbits[i] = new Rabbit();
}

// String representation of a hat

public String toString()
{
// Hat name first ...
String hatString = "\n" + hatName + " contains:\n";

for(int i = 0; i<rabbits.length; i++)
hatString += "\t" + rabbits[i] + " "; // Add the rabbits strings
return hatString;
}

private String hatName; //Name of the hat
private Rabbit rabbits[]; //Rabbits in the hat

// Nested class to define a rabbit
static class Rabbit
{
// A name is a rabbit name from rabbitNames followed by an integer

static private String[] rabbitNames = {"Floppsy", "Moppsy","Gnasher", "Thumper"};
static private int[] rabbitNamesCount = new int[rabbitNames.length];
private String name; // Name of the rabbit

//Constructor for a rabbit

public Rabbit()
{
int index = select.nextInt(rabbitNames.length); // Get random name
name = rabbitNames[index] + (++rabbitNamesCount[index]);
}

// String representation of a rabbit
public String toString()
{
return name;
}
}
}

Thanks in advance for any help you can provide.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 25 2008
Added on Aug 28 2008
2 comments
529 views