java如何获取机器名
答案:5 悬赏:50 手机版
解决时间 2021-11-30 23:37
- 提问者网友:棒棒糖
- 2021-11-30 12:03
java如何获取机器名
最佳答案
- 五星知识达人网友:孤独的牧羊人
- 2021-11-30 13:20
java获取机器名,主要是使用InterAddress类,如下代码:
package com.qiu.lin.he;
import java.net.InetAddress;
public class Ceshi {
public static void main(String[] args) {
InetAddress addr = null;
String address = "";
try {
addr = InetAddress.getLocalHost();//新建一个InetAddress类
address = addr.getHostName().toString();// 获得本机名称
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(address);
}
}结果如下:
package com.qiu.lin.he;
import java.net.InetAddress;
public class Ceshi {
public static void main(String[] args) {
InetAddress addr = null;
String address = "";
try {
addr = InetAddress.getLocalHost();//新建一个InetAddress类
address = addr.getHostName().toString();// 获得本机名称
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(address);
}
}结果如下:
全部回答
- 1楼网友:执傲
- 2021-11-30 18:22
你好,你可以按照下面这样得到ip,其前面就是缀着机器名的
String ip=InetAddress.getLocalHost().toString();
String ip=InetAddress.getLocalHost().toString();
- 2楼网友:逐風
- 2021-11-30 17:25
InetAddress.getLocalHost();
- 3楼网友:野慌
- 2021-11-30 15:56
gethostbyname()
gethostname()
GetComputerName()
gethostname()
GetComputerName()
- 4楼网友:底特律间谍
- 2021-11-30 14:43
//iplabel.java import java.net.*; import java.awt.*; import javax.swing.*; public class iplabel { JFrame frame; JLabel label1,label2; JPanel panel; String ip,address;
public void getip() { try { InetAddress addr = InetAddress.getLocalHost(); ip=addr.getHostAddress().toString;//获得本机IP address=addr.getHostName()toString;//获得本机名称
//System.out.println("addr=:"+String.valueOf(addr)); } catch(Exception e) { System.out.println("Bad IP Address!"+e); } }
public void showframe() { frame=new JFrame("my ip"); label1=new JLabel("this my ip"); label1.setText(ip); label2=new JLabel("this my address"); label2.setText(address); panel=new JPanel(); panel.add(label1); panel.add(label2); frame.getContentPane().add(panel);
frame.setSize(400,300); frame.setVisible(true); }
public static void main(String agrs[]) { iplabel myip=new iplabel(); myip.getip(); myip.showframe();
} }
public void getip() { try { InetAddress addr = InetAddress.getLocalHost(); ip=addr.getHostAddress().toString;//获得本机IP address=addr.getHostName()toString;//获得本机名称
//System.out.println("addr=:"+String.valueOf(addr)); } catch(Exception e) { System.out.println("Bad IP Address!"+e); } }
public void showframe() { frame=new JFrame("my ip"); label1=new JLabel("this my ip"); label1.setText(ip); label2=new JLabel("this my address"); label2.setText(address); panel=new JPanel(); panel.add(label1); panel.add(label2); frame.getContentPane().add(panel);
frame.setSize(400,300); frame.setVisible(true); }
public static void main(String agrs[]) { iplabel myip=new iplabel(); myip.getip(); myip.showframe();
} }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯