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

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


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

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