-1

I am new to Java, have searched on the net and this forum but unable to figure out why my code is not compiling? Any help will be highly appreciated.

//filename is TestHTTPConnection.java

class TestHTTPConnection {

public static void main (String[] args){

    String strUrl = "http://abc.com";

    try {
        URL url = new URL(strUrl);
        URLConnection Conn = url.openConnection();
        Conn.connect();

        assertEquals(URLConnection.HTTP_OK, Conn.getResponseCode());
    } catch (IOException e) {
        System.err.println("Error creating HTTP connection");
        e.printStackTrace();
        throw e;
    }
}
}

Compilation error - complains about "URL", "URLConnection" and "IOException".

4

3 回答 3

4

您需要导入这些类/接口!

于 2013-03-21T18:50:55.833 回答
2

你错过了两件事:

包装线:

package yourdomain.yourapp;

和进口清单:

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

大多数 Java 开发人员使用自动化所有这些的 IDE(例如 NetBeans、IntelliJ IDEA 或 Eclipse)

于 2013-03-21T18:52:46.473 回答
0

如果您不使用 IDE(Eclipse、NetBeans 等),请下载一个它会为您提供内容帮助并指出此类错误。

于 2013-03-21T18:57:21.747 回答