0

I have two java files or classes

One is a example called URLConnectionReader.java that does:

import java.net.*;
import java.io.*;

public class URLConnectionReader {

    public static void main(String[] args) throws Exception {
        URL oracle = new URL("http://www.oracle.com/");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

Second is Tester.java with:

public class Tester extends URLConnectionReader { 
   public static void main(String []args){

    URLConnectionReader.main(args);

    }  
}

All I wanted to do is execute, the main method in the URLConnectionReader.java in the Tester.java main method?

I get this error "unreported exception Exception; must be caught or declared to be thrown"

I plan to use the Tester.java as my main app and include various other methods or function.

What am I doing wrong here? It's frustrating as all the other examples are more complex code than this. I thought the "extend" will include the URLConnectionReader.class into the Tester.class no?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
johnny R
  • 9
  • 2
  • 2
    You need `public static void main(String []args) throws Exception {` in `Tester`. – Andy Turner Apr 04 '16 at 14:32
  • 1
    Note that you almost certainly don't need to extends `URLConnectionReader` in your `Tester` class. – Andy Turner Apr 04 '16 at 14:33
  • Thanks Andy, adding the "throws Exception" in Tester.java worked (bloody syntax). But i wanted to asked why i dont need to extend? how does Tester.java know i am calling the URLConnectionReader main method without any references too it?? if you know what i mean. Unless the java takes in to consideration all the files in the directory at the time the program is executed?? – johnny R Apr 04 '16 at 14:37

0 Answers0