Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

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!


0
anxx0018
Asked:
anxx0018
  • 5
  • 4
  • 2
1 Solution
 
bobbit31Commented:
public static void main(String[] args)
0
 
anxx0018Author Commented:
what does this mean?
0
 
anxx0018Author Commented:
Why does it need "String[] args"?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
wide_awakeCommented:
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)

0
 
bobbit31Commented:
and String[] args is just an array of strings specifying any command line parameters (if any) passed in...
0
 
wide_awakeCommented:
When you try to run a class using

> java FileHandle

the JVM does something similar to this (not exactly, but just to illustrate).

String[] args = new String[numberOfArguments]; // in this case new String[0]
for(int i=0;i<numOfArguments;i++)
   args[i] = argumentI;

FileHandle.main(args); // args is an empty String array

If you don't supply any arguments, it still passes in a String array, but the length of the array is zero.

When the JVM tried to do
FileHandle.main(args);

it couldn't find a method called "main" that took a String array as a parameter, so it threw the NoSuchMethod error.

That's what's going on "behind the scenes"
0
 
anxx0018Author Commented:
---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!



0
 
bobbit31Commented:
> 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
0
 
anxx0018Author Commented:
Excellently clear anwser! Thank you very much!
0
 
anxx0018Author Commented:
Thank you very much, bobbit31.
0
 
bobbit31Commented:
your welcome ;)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now