java tutorial - unknown host exception - java

I'm trying to read a URL using the tutorial : http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
But I'm getting below exception. www.google.com is a known host ?
Exception in thread "main" java.net.UnknownHostException: www.google.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:395)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:234)
at sun.net.www.http.HttpClient.New(HttpClient.java:307)
at sun.net.www.http.HttpClient.New(HttpClient.java:324)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
at java.net.URL.openStream(URL.java:1010)
at URLReader.main(URLReader.java:8)

Are you running behind a proxy ?
The article seems to suggest there is a configuration needed in that case.
Alternatively, the program might hang or you might see an exception stack trace. If either of the latter two events occurs, you may have to set the proxy host so that the program can find the Oracle server.

Are you behind a proxy? Try to set the proxy in eclipse or try
java -DproxySet=true -DproxyHost=10.0.0.14 -DproxyPort=6588 JavApp

I think its network error check your internet Connection....

Related

java.net.URLConnection fails to connect via http or https

I am debugging an issue with my local Fiji/ImageJ installation failing to update through the Help -> Update UI. The ImageJ code that triggers the issue is this updater code: https://github.com/imagej/imagej-ui-swing/blob/78a3180b6bc830166d15c73a01443c7a642c3908/src/main/java/net/imagej/ui/swing/updater/ImageJUpdater.java#L360-L370
I wrote minimal test case to reproduce this pointing JAVA_HOME at the Fiji/ImageJ application (/Applications/Fiji.app/java/macosx/zulu8.60.0.21-ca-fx-jdk8.0.322-macosx_x64/jre/Contents/Home).
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.io.IOException;
import java.net.HttpURLConnection;
class HelloWorld {
public static void main(String[] args) {
try {
final URL url = new URL("http://neverssl.com");
final URLConnection urlConn = url.openConnection();
final HttpURLConnection httpConn = (HttpURLConnection) urlConn;
final int code = httpConn.getResponseCode();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
This code results in the following error:
java.io.IOException: Error writing to server
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:705)
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:717)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1598)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1505)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at HelloWorld.main(HelloWorld.java:14)
If I change the URL to an https URL, the error changes to
javax.net.ssl.SSLException: Couldn't kickstart handshaking
at sun.security.ssl.Alert.createSSLException(Alert.java:127)
at sun.security.ssl.TransportContext.fatal(TransportContext.java:348)
at sun.security.ssl.TransportContext.fatal(TransportContext.java:291)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:449)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:410)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:197)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1577)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1505)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:352)
at HelloWorld.main(HelloWorld.java:14)
Suppressed: java.net.SocketException: Broken pipe (Write failed)
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
at sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:81)
at sun.security.ssl.TransportContext.fatal(TransportContext.java:379)
... 10 more
Caused by: java.net.SocketException: Broken pipe (Write failed)
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
at sun.security.ssl.SSLSocketOutputRecord.flush(SSLSocketOutputRecord.java:251)
at sun.security.ssl.HandshakeOutStream.flush(HandshakeOutStream.java:89)
at sun.security.ssl.ClientHello$ClientHelloKickstartProducer.produce(ClientHello.java:580)
at sun.security.ssl.SSLHandshake.kickstart(SSLHandshake.java:510)
at sun.security.ssl.ClientHandshakeContext.kickstart(ClientHandshakeContext.java:112)
at sun.security.ssl.TransportContext.kickstart(TransportContext.java:231)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:432)
... 8 more
Comparing a tcpdump filtered on the attempted host on this machine compared to a working one, I see early FIN packets being sent before any PSH packets are sent, so I think that is related to connections closing early.
On another machine running the same OS X version, I ran the same snippet pointing JAVA_HOME at the same directory, and had no issues.

Java Socket ConnectException [duplicate]

This question already has answers here:
What is a connection timeout during a http request
(2 answers)
Closed 27 days ago.
I get an exception when I run this code. Why?
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class MainClass {
public static void main(String[] args) throws Exception {
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
URL url = new URL("https://www.verisign.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
}
Exception:
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)
at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.getInputStream(HttpsURLConnectionOldImpl.java:204)
at java.net.URL.openStream(URL.java:1010)
at https.ssl.MainClass.main(MainClass.java:13)
We can't diagnose your networks for you. You need to do it yourself, or get your local admins to look at.
Things you should check before you bug your admins:
can you ping the host?
can you connect to http://www.verisign.com using a web browser?
can you connect to https://www.verisign.com using a web browser?
can you connect to http://www.verisign.com using your program?
can you connect to anything using your program?
The chances are that your problem is firewall related. My first guess would be that you don't have the correct environment variables or Java system properties set to tell the JVM to use a local proxy server for outgoing HTTP / HTTPS requests.
If it is not a problem with your settings, you will need to get help from someone local who can help you diagnose the problem.

Connection timed out. Why? [duplicate]

This question already has answers here:
What is a connection timeout during a http request
(2 answers)
Closed last month.
I get an exception when I run this code. Why?
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class MainClass {
public static void main(String[] args) throws Exception {
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
URL url = new URL("https://www.verisign.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
}
Exception:
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)
at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.getInputStream(HttpsURLConnectionOldImpl.java:204)
at java.net.URL.openStream(URL.java:1010)
at https.ssl.MainClass.main(MainClass.java:13)
We can't diagnose your networks for you. You need to do it yourself, or get your local admins to look at.
Things you should check before you bug your admins:
can you ping the host?
can you connect to http://www.verisign.com using a web browser?
can you connect to https://www.verisign.com using a web browser?
can you connect to http://www.verisign.com using your program?
can you connect to anything using your program?
The chances are that your problem is firewall related. My first guess would be that you don't have the correct environment variables or Java system properties set to tell the JVM to use a local proxy server for outgoing HTTP / HTTPS requests.
If it is not a problem with your settings, you will need to get help from someone local who can help you diagnose the problem.

Java RMI, Tomcat problem!

I am learning about Java RMI and have an example code to show how RMI can be used to pass objects to another virtual machines over a network.
Here are the classes and code I am using;
//interface
import java.rmi.*;
public interface MyRemote extends Remote
{
public String sayHello() throws RemoteException;
}
//My Remote Implementation Class
import java.rmi.*;
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote
{
public String sayHello()
{
return "Server says, 'Hey' ";
}
public MyRemoteImpl() throws RemoteException
{
}
public static void main (String[] args)
{
try
{
MyRemote service = new MyRemoteImpl();
Naming.rebind("Remote Hello", service);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
//My Remote Client Class
import java.rmi.*;
public class MyRemoteClient
{
public static void main (String [] args)
{
new MyRemoteClient().go();
}
public void go()
{
try{
MyRemote service = (MyRemote) Naming.lookup("rmi://127.0.0.1/Remote Hello");
String s = service.sayHello();
System.out.println(s);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
I keep getting java.net.MalformedURLException: invalid URL String: rmi://127.0.0.1/Remote Hello which I know is due to the 'Naming.lookup("rmi://127.0.0.1/Remote Hello")' code in the last class.
I have installed tomcat and tried placing the files in a directory, trying to get it to run on my local machine but I have had no joy.
I have created a JAR file of my project and tried placing that in a directory also and still no luck. I have heard about war files but currently the book I am learning from hasnt come to that yet...
Any tips on how I can get around this without the exception being called? Am I placing the files incorrectly in Tomcat? I have placed them in my C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps directory.
Any help or tips will be greatly appreciated.
UPDATE
Thanks for the advice, the String exception seems to have gone away but now I am lumbered with this.
java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:340)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:101)
at MyRemoteClient.go(MyRemoteClient.java:13)
at _SHELL57.run(_SHELL57.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:623)
at bluej.runtime.ExecServer$3.run(ExecServer.java:774)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:316)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:177)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:164)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:154)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:352)
at java.net.Socket.connect(Socket.java:569)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.(Socket.java:416)
at java.net.Socket.(Socket.java:199)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
2 things:
Change the name of the remote object to something without a space.
Are you running the server code first?
What does Tomcat have to do with RMI? Tomcat is a servlet/JSP engine that listens for HTTP requests on port 8080 by default.
RMI is a totally different protocol that has nothing to do with HTTP. The RMI daemon uses port 1099 by default.
I'd recommend looking at this.
Check your /etc/hosts file at the server. It should map localhost to 127.0.0.1 and your real hostname to your real IP address. If you have a configuration where you need the other way round, set the system property java.rmi.server.hostname to the correct IP address at the server JVM before exporting the remote object.

java.net.SocketException: Software caused connection abort: connect [duplicate]

This question already has answers here:
java.net.SocketException: Software caused connection abort: recv failed, with java.net.SocketException: Connection reset [duplicate]
(3 answers)
Closed 9 years ago.
I am trying to programmatically (Java) open a URL that points to a modem on the network. I am connected to the network and can ping the device as well as fetch the URL within a browser. However, programmatically, I get the following stack trace when trying to open the connection.
java.net.SocketException: Software caused connection abort: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:793)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1041)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
Also, the URL uses the https protocol.
Here is my code:
try {
URL jipmURL = new URL("https://xxx.xxx.xxx.xxx/login.cgi");
URLConnection urlConnection = jipmURL.openConnection();
InputStreamReader streamReader = new InputStreamReader(
jipmURLConnection.getInputStream());
BufferedReader in = new BufferedReader(streamReader);
StringBuffer stringBuffer = new StringBuffer();
String inputLine;
while ((inputLine = in.readLine()) != null) {
stringBuffer.append(inputLine);
}
System.out.println("Results: " + stringBuffer);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
Any help would be appreciated,
Steve
The exception message indicates that you're not even able to establish a TCP connection with the web server. It's difficult to guess what the exact problem is, since you're able to open the page from a browser, but could it simply be that you're running a software firewall, which refuses the Java process to connect to that address?

Categories

Resources