java.net.UnknownHostException: XXXX: 未知的名称或服务

今天在Linux启动项目的时候出现了这样一个异常:


查看主机名:


正常,那ping一下自己(主机名):


有问题了,修改hosts文件:


添加主机名和ip的映射关系:


保存后重新ping主机名,可以了:


重启网络服务:


重启项目看看:


启动成功!

发布了11 篇原创文章 · 获赞 0 · 访问量 978
展开阅读全文

java 异常java.net.UnknownHostException

12-05

package app21; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; public class SocketTest1 { public static void main(String[] args) { String host = "www.baidu.com"; String protocol = "https"; try { Socket socket = new Socket(protocol + "://" + host, 80); OutputStream os = socket.getOutputStream(); boolean autoflush = true; PrintWriter out = new PrintWriter(os, autoflush); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream())); // 将一个HTTP请求发送到Web服务器 out.println("GET / HTTP/1.1"); out.println("Host: " + host + ":80"); out.println("Connection: Close"); out.println(); // 读取响应信息 boolean loop = true; StringBuilder sb = new StringBuilder(8096); while (loop) { if (in.ready()) { int i = 0; while (i != -1) { i = in.read(); sb.append((char) i); } loop = false; } } // 将响应信息显示到out控制台 System.out.println(sb.toString()); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } 异常信息: java.net.UnknownHostException: https://www.baidu.com at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at java.net.Socket.connect(Socket.java:538) at java.net.Socket.<init>(Socket.java:434) at java.net.Socket.<init>(Socket.java:211) at app21.SocketTest1.main(SocketTest1.java:16) 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览