?
Solved

a small question about exception

Posted on 2003-03-28
11
Medium Priority
?
158 Views
Last Modified: 2010-03-31
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
Comment
Question by:anxx0018
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 8226874
public static void main(String[] args)
0
 

Author Comment

by:anxx0018
ID: 8227123
what does this mean?
0
 

Author Comment

by:anxx0018
ID: 8227145
Why does it need "String[] args"?
0
Industry Leaders: 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!

 
LVL 3

Expert Comment

by:wide_awake
ID: 8227190
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 8227195
and String[] args is just an array of strings specifying any command line parameters (if any) passed in...
0
 
LVL 3

Accepted Solution

by:
wide_awake earned 80 total points
ID: 8227313
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
 

Author Comment

by:anxx0018
ID: 8227321
---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
 
LVL 18

Expert Comment

by:bobbit31
ID: 8227347
> 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
 

Author Comment

by:anxx0018
ID: 8227360
Excellently clear anwser! Thank you very much!
0
 

Author Comment

by:anxx0018
ID: 8227377
Thank you very much, bobbit31.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8227430
your welcome ;)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question