集册 Java实例教程 获取此系统的主机名。

获取此系统的主机名。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

442
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
获取此系统的主机名。

/**

 * The contents of this file are subject to the AED Public Use License Agreement, Version 1.0 (the "License");

 * use in any manner is strictly prohibited except in compliance with the terms of the License.

 * The License is available at http://gatherdata.org/license.

 *

 * Copyright (c) AED.  All Rights Reserved

 */

//package com.nowjava;/** 时代Java公众号 - N o w J a  v a . c o m 提 供 **/

import java.net.InetAddress;

import java.net.UnknownHostException;


public class Main {

    public static void main(String[] argv) throws Exception {

        System.out.println(getHostName());

    }


    /**

     * Gets the hostname of this system.

     * 

     * @return hostname, or "localhost" if hostname could not be determined

     */

    public static String getHostName() {

        String hostname = null;

        try {

            InetAddress addr = InetAddress.getLocalHost();

            hostname = addr.getHostName();

        } 
展开阅读全文