开发者

THE CLASSICAL ---> Exception in thread "main" java.lang.NoSuchMethodError: main [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Exception in thread “main” Java.lang.NoSuchMethodError: main ??

public class InsertionSort
{
public static void main ( 开发者_StackOverflow中文版int[] a)
{
    int j;
    for( int p=1 ; p<a.length ; p++)
    {
        int tmp = a[p];
        for( j=p ; j>0 && tmp<a[j-1] ; j--)
        {
            a[j] = a[j-1];
        }
        a[j] = tmp;
    }
}
}

And this happens in Terminal. (I'm on a Mac if it matters) javac InsertionSort.java;java InsertionSort Exception in thread "main" java.lang.NoSuchMethodError: main


You need a proper main() to make the class runnable. A main method should have an array of string as the only argument, you have an array of ints.

So, to solve it, redeclare it to "public static void main(String[] args)" and do the integer parsing in the method. Neither java nor the OS will do that conversion for you.


public static void main (String[] arg)

main accepts array of string, not array of int.


The JVM lookt for a public static void main(String[]) signature, not for a main method that takes an int[] as argument.


It will run if you do it like this:

public static void main ( String[] args)
{
    int[] a = new int[args.length];
    for(int i = 0; i < args.length; i++){
        a[i]=Integer.parseInt(args[i]);
    }
    int j;
    for( int p=1 ; p<a.length ; p++)
    {
        int tmp = a[p];
        for( j=p ; j>0 && tmp<a[j-1] ; j--)
        {
            a[j] = a[j-1];
        }
        a[j] = tmp;
    }
}

A main method, needs a String array, you need an int array, so we'll just convert the one to the other.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜