user427390
user427390

Reputation:

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:

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.

Upvotes: 8

Views: 50211

Answers (5)

Dinosaure
Dinosaure

Reputation: 135

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

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.

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

Upvotes: 0

CurtainDog
CurtainDog

Reputation: 3205

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

Upvotes: 6

Adeel Ansari
Adeel Ansari

Reputation: 39887

Rename the file as DNSLookUp.java and import appropriate classes.

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

Upvotes: 4

vanza
vanza

Reputation: 9903

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

import java.net.*;

To import all classes from the java.net package.

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

Upvotes: 0

Reese Moore
Reese Moore

Reputation: 11640

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

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

At the top of the file

Upvotes: 16

Related Questions