Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exception in thread

Status
Not open for further replies.

zdarma

IS-IT--Management
Jun 30, 2003
5
CA
I keep getting the following error when I try to see if my script works Exception in thread "main" java.lang.NoSuchMethodError: main

Here is the script :
class Pizza {
int toppings;
int extras;
Pizza() {
toppings=1;
extras=3;
}
Pizza(int p, int c) {
toppings=p;
extras=c;
}
Pizza all=new Pizza(10,11);
Pizza none=new Pizza();
}

I have no problem doing a javac Pizza.java. Its just when I do a java Pizza that the error appears. I looked at the previous thread that had a similar error, but the only thing I had in common with the other person's ters was when I did a java -cp .Pizza which did nothing at all, but give me argument types.

I'm quite new to Java and have only just started. If anyone can give me a hand, it would be greatly appreciated.

Thanks in advance,
Z.

 
Java is looking for the
Code:
main
method (which is what the exception says), which isn't in your Pizza class file.

For a Java program to run there needs to be a start point, an entry point, into the program. This is provided by the
Code:
main
method.

the code looks will be similar to this
Code:
class ExampleClass
{
   public static void main(String[] args)
   {
      System.out.println("The start of the program");
   }
}

for your .java file to be executed you will need to add the
Code:
 public static void main(String[] args)
method, and then inside that method you need to create your Pizza objects.

Or alternately create a new object with the
Code:
main
methods which calls the Pizza object.


Clear as mud I hope.

-------------------------------------------
There are no onions, only magic
-------------------------------------------
 
check out this thread:-

Thread269-575107

~za~
You can't bring back a dead thread!
 
Thanks for the help, it worked like a charm.

However, I'm curious as to why I was able to compile the script without getting any errors until I tried to run the script. Is it because the script basically did nothing or does even a script that does nothing need the ...static void main... ?

Thx,
Z.
 
when you run the program, java.exe looks for an entry point; namely this particular static method. this is a requirement. you can compile many classes(in separate files) without getting any errors, assuming everything is syntactically correct. However, at run time you have to have this ONE method to let JVM know that this is the "door" to your program.

~za~
You can't bring back a dead thread!
 
Thanks for the infor max.

Z.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top