Java编译器错误:“公共类型..必须在它自己的文件中定义"? [英] Java compiler error: "public type .. must be defined in its own file"?

查看:24
本文介绍了Java编译器错误:“公共类型..必须在它自己的文件中定义"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译这个:

public class DNSLookUp {
    public static void main(String[] args)   {
        InetAddress hostAddress;
        try  {
            hostAddress = InetAddress.getByName(args[0]);
            System.out.println (hostAddress.getHostAddress());
        }
        catch (UnknownHostException uhe)  {
            System.err.println("Unknown host: " + args[0]);
        }
    }
}

我使用了 javac dns.java,但出现了一堆错误:

I used javac dns.java, but I am getting a mess of errors:

dns.java:1: error: The public type DNSLookUp must be defined in its own file
    public class DNSLookUp {
                 ^^^^^^^^^
dns.java:3: error: InetAddress cannot be resolved to a type
    InetAddress hostAddress;
    ^^^^^^^^^^^
dns.java:6: error: InetAddress cannot be resolved
    hostAddress = InetAddress.getByName(args[0]);
                  ^^^^^^^^^^^
dns.java:9: error: UnknownHostException cannot be resolved to a type
    catch (UnknownHostException uhe)  {
           ^^^^^^^^^^^^^^^^^^^^
4 problems (4 errors)

我以前从未编译/做过 Java.我只需要这个来测试我的其他程序结果.有任何想法吗?我正在 Linux 机器上编译.

I have never compiled/done Java before. I only need this to test my other programs results. Any ideas? I am compiling on a Linux machine.

推荐答案

文件需要调用 DNSLookUp.java 并且你需要把:

The file needs to be called DNSLookUp.java and you need to put:

import java.net.InetAddress;
import java.net.UnknownHostException;    

在文件顶部

这篇关于Java编译器错误:“公共类型..必须在它自己的文件中定义"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆