-1

Possible Duplicate:
Exception in thread “main” java.lang.NoSuchMethodError: main

My main syntax is correct. What else could be the problem?

public class BuildHeap
{       
  int a[]={1,2,6,3,5,1,7,8,4,9};

  public void build()
  {
      for(int i=5;i<=1;i--)
      {
         heapify(a,i);
      }
  }

  public void heapify(int a[],int i)
  { 
    System.out.print("hello");
    int j,temp,rchild,lchild;
    if(i<5)
     {
        if(2*i<5)
            lchild=a[(2*i)+1];
        if((2*i)+1<4)
            rchild=a[(2*i)+2];

        if(lchild>rchild)
            j=(2*i)+1;
        else
            j=(2*i)+2;

        if(a[i]<a[j])
         {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
            heapify(a,j);

} } }

class Heap
{
    public static void main(String[] args) 
    {
        BuildHeap bh=new BuildHeap();
        bh.build();
        for(int i=0;i<10;i++)
            System.out.print(bh.a[i]+" ");
    }
}
Community
  • 1
  • 1
Ava
  • 5,083
  • 23
  • 53
  • 81
  • 1
    Are you trying to run `Heap` or `BuildHeap` as the main class? `BuildHeap` doesn't have a `main`, which may cause this problem. Also, `Heap` is not marked `public`, which may have something to do with this. – templatetypedef Feb 07 '11 at 02:18
  • This Community Wiki question lists the possible causes of this common problem: http://stackoverflow.com/questions/5407250/causes-of-java-lang-nosuchmethoderror-main-exception-in-thread-main – Stephen C Jun 28 '11 at 14:39

2 Answers2

3

You need to put the main method in the public class BuildHeap (not the package-private class Heap). Or maybe you just specified the wrong class name on the command line. Again, main is in Heap, not BuildHeap.

Thilo
  • 241,635
  • 91
  • 474
  • 626
1

The most probable cause should be that your class name and file name doesnot match. Create a new file containing the class Heap and have the filename as Heap.java. Keep BuildHeap class in a different file in the same package. Run Heap. It should work. Else change the BuildHeap class from public class and make the Heap class as public.

The better way would be to separate the classes in different files.

aNish
  • 1,049
  • 6
  • 11