简体   繁体   English

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

[英]Java compiler error: “public type .. must be defined in its own file”?

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.我以前从未编译/做过 Java。 I only need this to test my other programs results.我只需要这个来测试我的其他程序结果。 Any ideas?有什么想法吗? I am compiling on a Linux machine.我正在 Linux 机器上编译。

The file needs to be called DNSLookUp.java and you need to put:该文件需要称为DNSLookUp.java并且您需要放置:

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

At the top of the file在文件顶部

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)安装选择的 IDE(Netbeans 很容易上手)
  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惊叹于您可用工具的强大功能

Rename the file as DNSLookUp.java and 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]);
        }
    }
}

You need to import the classes you're using.您需要导入正在使用的类。 eg:例如:

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.在名为 dns.java 的文件中也不能有公共类 DNSLookUp。 Looks like it's time for a Java tutorial...看起来是时候学习 Java 教程了...

Coming from "The public type <<classname>> must be defined in its own file" error in Eclipse which was marked as duplicate.来自Eclipse中标记为重复的“公共类型 <<classname>> 必须在其自己的文件中定义”错误

I'm thus answering this "duplicate" here: On Eclipse, you can prepare all your public class es in one file, then right-clic -> Refactor -> Move Type to New File .因此,我在这里回答这个“重复”:在 Eclipse 上,您可以在一个文件中准备所有public class ,然后right-clic -> Refactor -> Move Type to New File That's not exactly the right workaround (this won't let you have multiple public class es in one file at compile time), but that will let you move them around in their proper files when you'll be ready.这不是完全正确的解决方法(这不会让您在编译时在一个文件中拥有多个public class ),但是当您准备好时,这将允许您在适当的文件中移动它们。

Coming from C#, that's an annoying enforced limitation for do-forgettable (tutorial) classes, nonetheless a good habit to have.来自 C#,对于可忘记的(教程)类来说,这是一个令人讨厌的强制限制,但仍然是一个好习惯。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java:4:错误:公用类型NumberAdder必须在其自己的文件中定义 - java:4: error: The public type NumberAdder must be defined in its own file 枚举类型“公共类型EngineType必须在其自己的文件Car.java中定义”的Java编译错误 - Java compilation Error for an enum type “The public type EngineType must be defined in its own file Car.java” 公共类型必须在其自己的文件中定义 - the public type must be defined in its own file 文件将无法运行:“公共类型EchoTest必须在其自己的文件中定义” - File will not run: “The public type EchoTest must be defined in its own file” 公共类型 PlayerQuitListener 必须在它自己的文件中定义 - The public type PlayerQuitListener must be defined in its own file 公共类型Dog必须在其自己的文件中定义 - The public type Dog must be defined in its own file 公共类型SalutonFrame必须在其自己的文件中定义 - The public type SalutonFrame must be defined in its own file 禁用强制执行“必须在自己的文件中定义公共类型xyz” - Disable enforcement of “The public type xyz must be defined in its own file” 错误:公用类型TodoListItemView必须在其自己的文件中定义。 安卓 日食 - Error: The public type TodoListItemView must be defined in its own file. Android. Eclipse “必须在其自己的文件中定义公共类型”,但文件名和类名相同 - “The public type must be defined in its own file” but the file name and the class name is the same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM