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

问题描述

限时删除!!

我试图编译这个:

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 to put:

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

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

在文件顶部

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

1403页,肝出来的..

09-06 15:04