wood burning stoves 2.0*
The moose likes Java in General and the fly likes Connection refused on localhost Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Connection refused on localhost" Watch "Connection refused on localhost" New topic

Connection refused on localhost

Rudolf Lomniczi

Joined: Nov 02, 2010
Posts: 3

I faced an annoying and strange problem, I have no more idea how to solve it.
Problem: It's is a very simple situation. An Apache Tomcat runs on my machine, it listens on 8081 port. I want to acces this URL hosted by Tomcat from Java:


The sample code:

But it doesn't work, neither from Eclipse nor java command line. I get the following exception:

Exception in thread "main" java.net.ConnectException: Connection refused: 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: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:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:323)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:975)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:916)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:841)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1177)
at com.ind.bb.tools.URLTest.main(URLTest.java:31)

I can access this URL from any browsers. No http proxy set. Tomcat is running:

D:\programs\apache-tomcat-5.5.31\bin>netstat -a | grep 8081

Java version is jdk1.6.0_22, OS is Windows 7. I tried this URL's too:

Could somebody help me?
Tom Reilly

Joined: Jun 01, 2010
Posts: 618
I'm confused. You say that you want to access:


But then you try in your code:

Then at the bottom of your post, you say you tried:

But don't say where you tried it or whether it succeeded.
Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

Windows 7, is it? Then perhaps there's an internal firewall entry preventing anything from connecting to that port.

You could try the Telnet test: go to a command line screen and do this:

Rudolf Lomniczi

Joined: Nov 02, 2010
Posts: 3
Dear Tom,

Sry, I did not write enough information about call url. The answer: none of URLs were successful.

Dear Paul,

I tried telnet formerly and it was successful.

But problem has been solved yesterday: I used wrong Tomcat version: I had to use 6.x instead of 5.5.31. I don't know what is the real cause, but Tomcat 6.x works fine.

Thanks for help!
Rob Spoor

Joined: Oct 27, 2005
Posts: 19655

Rudolf Lomniczi wrote:Sry

Sorry to nitpick, but UseRealWords: sorry.

How To Ask Questions How To Answer Questions
Rudolf Lomniczi

Joined: Nov 02, 2010
Posts: 3
All right, no problem I'll follow it. I'm a new user.
I agree. Here's the link: http://aspose.com/file-tools
subject: Connection refused on localhost
Similar Threads
Getting Connection Refused on HttpURLConnection
Access to website using Java Code
how to resolve java.net.ConnectException: Connection refused: connect
SocketException in WSAD
Connection Refused Exception