开发者

Different results when compiling with command prompt and BlueJ

开发者 https://www.devze.com 2023-03-21 05:23 出处:网络
I\'m just starting Java ... again. I just made a simple program class first { 开发者_运维问答public static void main()

I'm just starting Java ... again. I just made a simple program

class first
{
开发者_运维问答    public static void main()
    {
        System.out.println("Hello!");
    }
}

This runs perfectly fine in BlueJ but it gives an error during run-time when running from command prompt.

This is the error Exception in thread "main" java.lang.NoSuchMethodError: main

It's because I didn't give String args[] in the main parameter list

Till now, I used to give it subconsciously. I know that the string array contains all the parameter values when running but then why is it running in BlueJ?

(BlueJ is a student-friendly Java editor and compiler)


Your program is valid and will compile to the same thing whether you compile from BlueJ or from the command line.

However, blueJ will let you run any static method in a class (so you can test your functions) where as the command line java command will (only) look for a special main method to run. This main method tages a String array with all the command line parameters and your program should look like this even though you don't use these command line parameters:

class first
{
    public static void main(String[] args)
    {
        System.out.println("Hello!");
    }
}
0

精彩评论

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