0

I want to get the server hostname using Java while starting the server.

Before that i was used HTTP servlet request but I cannot get the request during start the server it's shows null pointer exception.

Can you help to get the solution for this.

I want the solution like this.

localhost:8080 or some other ip server with port

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126

1 Answers1

0

your can try this to get hostname

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

public class HostName {
public static void main(String a[]){
    try {
        InetAddress myHost = InetAddress.getLocalHost();
        System.out.println(myHost.getHostName());
    } catch (UnknownHostException ex) {
        ex.printStackTrace();
    }
  }
}

See: https://stackoverflow.com/a/17956000/768795