Java编译器错误:“公共类型..必须在它自己的文件中定义”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4084005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:25:23  来源:igfitidea点击:

Java compiler error: "public type .. must be defined in its own file"?

javacompiler-errors

提问by

I am trying to compile this:

我正在尝试编译这个:

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]);
        }
    }
}

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

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

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)

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.

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

采纳答案by Reese Moore

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

该文件需要被调用DNSLookUp.java,你需要把:

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

At the top of the file

在文件顶部

回答by vanza

You need to import the classes you're using. e.g.:

您需要导入正在使用的类。例如:

import java.net.*;

导入 java.net.*;

To import all classes from the java.net package.

从 java.net 包中导入所有类。

You also can't have a public class DNSLookUp in a file named dns.java. Looks like it's time for a Java tutorial...

在名为 dns.java 的文件中也不能有公共类 DNSLookUp。看起来是时候学习 Java 教程了...

回答by Adeel Ansari

Rename the file as DNSLookUp.javaand import appropriate classes.

将文件重命名为DNSLookUp.java并导入适当的类。

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

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]);
        }
    }
}

回答by CurtainDog

The answers given here are all good, but given the nature of these errors and in the spirit of 'teach a man to fish, etc, etc':

这里给出的答案都很好,但考虑到这些错误的性质,本着“教人钓鱼等”的精神:

  1. Install IDE of choice (Netbeans is an easy one to start with)
  2. Setup your code as a new project
  3. Click the lightbulb on the line where the error occurs
  4. Select the fix you'd like
  5. Marvel at the power of the tools you have available
  1. 安装选择的 IDE(Netbeans 很容易上手)
  2. 将您的代码设置为新项目
  3. 单击发生错误的行上的灯泡
  4. 选择您想要的修复
  5. 惊叹于您可用工具的强大功能

回答by Dinosaure

Coming from "The public type <<classname>> must be defined in its own file" error in Eclipsewhich was marked as duplicate.

来自Eclipse中标记为重复的“公共类型 <<classname>> 必须在其自己的文件中定义”错误

I'm thus answering this "duplicate" here: On Eclipse, you can prepare all your public classes in one file, then right-clic -> Refactor -> Move Type to New File. That's not exactly the right workaround (this won't let you have multiple public classes in one file at compile time), but that will let you move them around in their proper files when you'll be ready.

因此,我在这里回答这个“重复”:在 Eclipse 上,您可以public class在一个文件中准备所有es,然后right-clic -> Refactor -> Move Type to New File. 这不是完全正确的解决方法(这不会让您public class在编译时在一个文件中有多个es),但是当您准备好时,这将允许您在适当的文件中移动它们。

Coming from C#, that's an annoying enforced limitation for do-forgettable (tutorial) classes, nonetheless a good habit to have.

来自 C#,对于可忘记的(教程)类来说,这是一个令人讨厌的强制限制,但仍然是一个好习惯。