title: Error in Java Rest Service : Connection refused: connect
body: <p>I am trying to create a rest service. But I got the following error after running.</p>
  
  <pre><code>Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
      at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:151)
      at com.sun.jersey.api.client.Client.handle(Client.java:648)
      at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
      at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
      at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507)
      at astral.restservice.client.Test.main(Test.java:20)
  Caused by: java.net.ConnectException: Connection refused: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
      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.&lt;init&gt;(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 Source)
      at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at java.net.HttpURLConnection.getResponseCode(Unknown Source)
      at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:249)
      at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
      ... 5 more
  </code></pre>
  
  <p>Following is my web.xml : </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"&gt;
    &lt;display-name&gt;astral.restservice&lt;/display-name&gt;
    &lt;servlet&gt;
      &lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt;
      &lt;init-param&gt;
        &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
        &lt;param-value&gt;astral.restservice&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Hello.java</p>
  
  <pre><code>package astral.restservice;
  
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.MediaType;
  
  // Plain old Java Object it does not extend as class or implements 
  // an interface
  
  // The class registers its methods for the HTTP GET request using the @GET annotation. 
  // Using the @Produces annotation, it defines that it can deliver several MIME types,
  // text, XML and HTML. 
  
  // The browser requests per default the HTML MIME type.
  
  //Sets the path to base URL + /hello
  @Path("/hello")
  public class Hello {
  
    // This method is called if TEXT_PLAIN is request
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
      return "Hello Jersey";
    }
  
    // This method is called if XML is request
    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
      return "&lt;?xml version=\"1.0\"?&gt;" + "&lt;hello&gt; Hello Jersey" + "&lt;/hello&gt;";
    }
  
    // This method is called if HTML is request
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
      return "&lt;html&gt; " + "&lt;title&gt;" + "Hello Jersey" + "&lt;/title&gt;"
          + "&lt;body&gt;&lt;h1&gt;" + "Hello Jersey" + "&lt;/body&gt;&lt;/h1&gt;" + "&lt;/html&gt; ";
    }
  
  } 
  </code></pre>
  
  <p>Test.java</p>
  
  <pre><code>package astral.restservice.client;
  
  import java.net.URI;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.UriBuilder;
  import com.sun.jersey.api.client.Client;
  import com.sun.jersey.api.client.ClientResponse;
  import com.sun.jersey.api.client.WebResource;
  import com.sun.jersey.api.client.config.ClientConfig;
  import com.sun.jersey.api.client.config.DefaultClientConfig;
  public class Test {
    public static void main(String[] args) {
      ClientConfig config = new DefaultClientConfig();
      Client client = Client.create(config);
      WebResource service = client.resource(getBaseURI());
      // Fluent interfaces
      System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());
      // Get plain text
      System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN).get(String.class));
      // Get XML
      System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_XML).get(String.class));
      // The HTML
      System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_HTML).get(String.class));
  
  
    }
  
    private static URI getBaseURI() {
      return UriBuilder.fromUri("http://localhost:8080/astral.restservice").build();
    }
  
  } 
  </code></pre>
  
  <p>This is my project structure
  <img src="https://i.stack.imgur.com/iHFqD.png" alt="enter image description here"></p>
  
  <p>Why I am getting this error? Thanks in advance.</p>
  
  <p>I got the following error from browser after run it using tomcat v7</p>
  
  <pre><code>HTTP Status 500 - Servlet.init() for servlet Jersey REST Service threw exception
  
  type Exception report
  
  message Servlet.init() for servlet Jersey REST Service threw exception
  
  description The server encountered an internal error that prevented it from fulfilling this request.
  
  exception
  
  javax.servlet.ServletException: Servlet.init() for servlet Jersey REST Service threw exception
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
      org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
      java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      java.lang.Thread.run(Unknown Source)
  
  root cause
  
  com.sun.jersey.spi.inject.Errors$ErrorMessagesException
      com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
      com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
      com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
      com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:695)
      com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:690)
      com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:438)
      com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:287)
      com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:587)
      com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:213)
      com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:342)
      com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:516)
      javax.servlet.GenericServlet.init(GenericServlet.java:160)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
      org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
      java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      java.lang.Thread.run(Unknown Source)
  
  note The full stack trace of the root cause is available in the Apache Tomcat/7.0.39 logs.
  Apache Tomcat/7.0.39
  </code></pre>
  
  <p>I tried to run server using command prompt I got the following error,
  I set CATALINA_HOME,JAVA_HOME,CLASSPATH and add PATH also.</p>
  
  <p>**</p>
  
  <pre><code>D:\apache-tomcat-7.0.39\bin&gt;startup
  The CATALINA_HOME environment variable is not defined correctly
  This environment variable is needed to run this program
  
  D:\apache-tomcat-7.0.39\bin&gt;startup.bat
  The CATALINA_HOME environment variable is not defined correctly
  This environment variable is needed to run this program
  
  D:\apache-tomcat-7.0.39\bin&gt;catalina.bat
  The CATALINA_HOME environment variable is not defined correctly
  This environment variable is needed to run this program
  
  D:\apache-tomcat-7.0.39\bin&gt;
  </code></pre>
  
  <p>**</p>