[英]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':这里给出的答案都很好,但考虑到这些错误的性质,本着“教人钓鱼等”的精神:
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.