B
B
Boris Onofrey2014-09-25 17:30:21
Java
Boris Onofrey, 2014-09-25 17:30:21

How to get rid of java.net.UnknownHostException: vk.com?

There is a code

URL url = new URL("http://vk.com");
        BufferedInputStream inputStream = new BufferedInputStream(url.openStream());
        FileOutputStream outputStream = new FileOutputStream("pathToSave/1.html");
        byte[] buffer = new byte[4096];
        while (inputStream.read(buffer) > 0) {
            outputStream.write(buffer);
        }
        inputStream.close();
        outputStream.close();

When run(in ide(idea), on the command line), the program throws an exception
D:\Clouds\Dropbox\Projects\IdeaProjects\DownloadAgent\out\artifacts\DownloadAgen
t_jar>"C:\Program Files\Java\jre7\bin\java.exe" -jar DownloadAgent.jar
Exception in thread "main" java.net.UnknownHostException: vk.com
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at sun.net.NetworkClient.doConnect(Unknown Source)
        at sun.net.www.http.HttpClient.openServer(Unknown Source)
        at sun.net.www.http.HttpClient.openServer(Unknown Source)
        at sun.net.www.http.HttpClient.<init>(Unknown Source)
        at sun.net.www.http.HttpClient.New(Unknown Source)
        at sun.net.www.http.HttpClient.New(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown
Source)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Sour
ce)
        at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown So
urce)
        at java.net.URL.openStream(Unknown Source)
        at com.redprojects.net.downloadAgent.Main.main(Main.java:14)

An error is thrown at any url.
Already changed the JDK (yuzal and 8 and 7 versions). Nothing helps.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ruslan Lopatin, 2014-09-25
@RedCreepster

I met this one on Raspbian. So I didn't understand what was going on. I had to register the desired host in /etc/hosts. Thankfully only one was needed.
If the addresses are resolved safely outside of Java, then there may be something wrong with the Java network settings . These settings are written in JAVA_HOME/jre/lib/net.properties or on the command line. You can try to play around. For example, set networkaddress.cache.ttl=0

R
Renat Iliev, 2014-12-10
@IzeBerg

Check on the command line:
If there are errors, it's not in the code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question