Link to home
Start Free TrialLog in
Avatar of anxx0018
anxx0018

asked on

a small question about exception

I am new to Java Programming. I am trying to read from a file, but have the runtime error. Can anybody help me with this? Thank you very much!

Here is my code:

import java.lang.*;
import java.io.*;

class FileHandle {
    public static void main() throws Exception {
     File srcFile = new File("/tmp_mnt/home/anqian/DATFILES/d19981125.dat");
     BufferedReader in = new BufferedReader(new FileReader(srcFile));
         String text = in.readLine();
         System.out.println(text);
         System.out.println(text.length());
         in.close();
    }
}

It can pass the compilation, but has Exception error in runtime. The error is:

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



Thank you very much for your help!


Avatar of bobbit31
bobbit31
Flag of United States of America image

public static void main(String[] args)
Avatar of anxx0018
anxx0018

ASKER

what does this mean?
Why does it need "String[] args"?
main() is not the same as main(String[] args).

they're actually different methods, due to polymorphism.

The entry-point to a class *must* be
public static void main(String[] args)

and String[] args is just an array of strings specifying any command line parameters (if any) passed in...
ASKER CERTIFIED SOLUTION
Avatar of wide_awake
wide_awake

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
---The entry-point to a class *must* be
public static void main(String[] args)

Does it mean whenever there is a new defined class, it is always ..... main (String[] args), even there is no input parameters passed to the main function?

Thank you a lot!



> Does it mean whenever there is a new defined class, it is always ..... main (String[] args), even there is no input parameters passed to the main function?

yes
Excellently clear anwser! Thank you very much!
Thank you very much, bobbit31.
your welcome ;)