Example usage for java.net InetAddress getLocalHost

List of usage examples for java.net InetAddress getLocalHost

Introduction

In this page you can find the example usage for java.net InetAddress getLocalHost.

Prototype

public static InetAddress getLocalHost() throws UnknownHostException 

Source Link

Document

Returns the address of the local host.

Usage

From source file:Main.java

public static String getMACAdress() {
    InetAddress ip;// w  w  w. jav  a  2 s.c o m
    try {
        ip = InetAddress.getLocalHost();
        NetworkInterface network = NetworkInterface.getByInetAddress(ip);
        byte[] mac = network.getHardwareAddress();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
        }
        return sb.toString();

    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static void mkView(String viewTag) {
    try {/*from  ww w .j  a va  2 s .  c  o m*/
        Process pr = Runtime.getRuntime().exec("cleartool mkview -tag " + viewTag + " \\\\"
                + InetAddress.getLocalHost().getHostName() + "\\viewstore\\" + viewTag + ".vws");
        pr.waitFor();
        pr.destroy();
    } catch (Exception e) {
        throw new RuntimeException("cleartool mkview error!", e);
    }
}

From source file:Main.java

/**
 * Attempts to retrieve a "connectable" address for this device that other devices on the network can use to connect to a local proxy.
 * This is a "reasonable guess" that is suitable in many (but not all) common scenarios.
 * TODO: define the algorithm used to discover a "connectable" local host
 * @return a "reasonable guess" at an address that can be used by other machines on the network to reach this host
 *//*from www  . j  a v a  2  s  . c o  m*/
public static InetAddress getConnectableAddress() {
    try {
        return InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        throw new RuntimeException("Could not resolve localhost", e);
    }
}

From source file:com.espe.distribuidas.protocolocajero.pc.Originador.java

public static String getOriginador(String c) {
    String origen = "";
    try {//from   w  w  w.java2  s  .c om
        InetAddress localHost = InetAddress.getLocalHost();
        origen = localHost.getHostAddress();
    } catch (UnknownHostException ex) {
        Logger.getLogger(Originador.class.getName()).log(Level.SEVERE, null, ex);
    }
    origen = origen + "@" + c;
    origen = StringUtils.rightPad(origen, 20, "0");
    return origen;
}

From source file:Main.java

public static String getLocalIpAddress() throws Exception {
    String ipAddress = null;/*from w  w  w.j  a  v a2  s.c  o m*/

    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();

    while (en.hasMoreElements()) {
        NetworkInterface e = en.nextElement();
        Enumeration<InetAddress> addresses = e.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) {
                ipAddress = address.getHostName();
                break;
            }
        }
    }

    if (ipAddress == null) {
        ipAddress = InetAddress.getLocalHost().getHostAddress();
    }

    return ipAddress;
}

From source file:Main.java

public static String getHostAddress() throws UnknownHostException {
    InetAddress address = null;/*w w w  . j ava  2 s.c  om*/
    address = InetAddress.getLocalHost();
    return address.getHostAddress();
}

From source file:Main.java

/**
 * DNS reverse name lookup.//from   w  ww.  jav  a  2s  . c om
 *
 * @param includeDomain <code>true</code> if the whole FQDN should be returned, instead of just the first (host) part.
 * @return The resolved host (and domain-) name, or "UNKNOWN HOST" if resolution failed.
 */
public static String getLocalHostName(boolean includeDomain) {
    try {
        String hostname = InetAddress.getLocalHost().getHostName();
        return includeDomain ? hostname
                : hostname.indexOf(".") != -1 ? hostname.substring(0, hostname.indexOf(".")) : hostname;

    } catch (Exception ex) {
        // Return a dummy String
        return "UNKNOWN HOST";
    }
}

From source file:com.util.FileService.java

/**
 * sets the folder paths for the file locations depending on the host 
 * computer the application is running from.
 * /*from  w  w w  .j  av a 2 s. c  o m*/
 * @return boolean for file paths not available
 */
public static boolean setFolderPaths() {
    try {
        switch (InetAddress.getLocalHost().getHostName()) {
        case "Parkers-MacBook-Air.local":
        case "Parkers-Air":
            Global.setScanPath("/Users/parkerjohnston/Desktop/SERB/Scan/");
            Global.setEmailPath("/Users/parkerjohnston/Desktop/SERB/Email/");
            Global.setActivityPath("/Users/parkerjohnston/Desktop/SERB/Activity/");
            return true;
        case "Alienware15":
        case "Sniper":
            Global.setScanPath("C:\\SERB\\Scan\\");
            Global.setEmailPath("C:\\SERB\\Email\\");
            Global.setActivityPath("C:\\SERB\\Activity\\");
            return true;
        //                case "CS12-SRB-ES1":  //OUR SERB Machine
        //                    Global.setScanPath("C:\\SERB\\Scan\\");
        //                    Global.setEmailPath("C:\\SERB\\Email\\");
        //                    Global.setActivityPath("C:\\SERB\\Activity\\");
        //                    return true;
        default:
            Global.setScanPath("G:\\SERB\\Scan\\");
            Global.setEmailPath("G:\\SERB\\Email\\");
            Global.setActivityPath("G:\\SERB\\Activity\\");
            return true;
        }

    } catch (UnknownHostException ex) {
        ExceptionHandler.Handle(ex);
        return false;
    }
}

From source file:com.boundary.plugin.sdk.PluginUtil.java

/**
 * Returns the localhost name the JVM is running on
 * //from www  .  j a  v  a  2s  . co  m
 * @return {@link String} local host name
 */
public static String getHostname() {
    String hostname = null;
    try {
        InetAddress addr;
        addr = InetAddress.getLocalHost();
        hostname = addr.getHostName();
    } catch (Exception e) {
        LOG.info(e.toString());
    }
    return hostname;
}

From source file:com.yahoo.pulsar.broker.ServiceConfigurationUtils.java

public static String unsafeLocalhostResolve() {
    try {/*  w  w w. ja v  a 2 s.  c om*/
        return InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new IllegalStateException("Failed to resolve localhost name.", ex);
    }
}