Java HTTP Proxy Settings


For local networks within an organization, access to the public-domain Internet is often via a HTTP Proxy. This article talks about the HTTP proxy settings for the Java environment. I did not find a good document on the Web to describe these settings; Had to discover many of them by trial-and-error. Hence this article.


HTTP Proxy, Java Proxy Settings, Tomcat, Application Server, Servlets, HTTP Proxy Authentication for Java, Java Application Proxy Settings


  • Your Java client runs on a machine on the Local network – Private LAN. The client could be a standalone application, or a servlet hosted on a web container like Tomcat
  • Your code access an external resource using HTTP. For example, invoking an external Web Service.
  • Your HTTP call needs to tunnel through the HTTP proxy (using SOCKS authentication). Even if authentication is not required, you would still need to configure the URL and the Port of your HTTP proxy.


Use one of the methods below for your JVM proxy settings. Try an alternate method if any particular method does not work. In most cases, you should not require any change the pre-compiled Java code for proxy settings. JVM’s environment settings should be enough to fix this problem.

Command Line JVM Settings

The proxy settings are given to the JVM via command line arguments:


$ java -Dhttp.proxyHost=proxyhostURL
-Dhttp.proxyPassword=somePassword javaClassToRun

Setting System Properties in Code

Add the following lines in your Java code so that JVM uses the proxy to make HTTP calls. This would, of course, require you to recompile your Java source. (The other methods do not require any recompilation.):

System.getProperties().put("http.proxyHost", "someProxyURL");
System.getProperties().put("http.proxyPort", "someProxyPort");
System.getProperties().put("http.proxyUser", "someUserName");
System.getProperties().put("http.proxyPassword", "somePassword");

Don’t hardcode the proxy settings in your source. Read these settings from a configurable text file, so your users can configure them. You might also need to set this property:

System.getProperties().put("proxySet", "true");


System.getProperties().put("http.proxySet", "true");

Tomcat Settings:

Append these properties to the file in Tomcat: ${CATALINA_OME}/conf/ file:


Tomcat Settings: catalina.bat

Add all the parameters defined above in the ${CATALINA_HOME}/bin/catalina.bat (for Windows) or ${CATALINA_HOME}/bin/catalina.bat (for *nix):

JAVA_OPTS="-Dhttp.proxyHost=yourProxyURL ..."

(Each option is seperated by spaces.)


28 thoughts on “Java HTTP Proxy Settings

  1. Pingback: Aaron Johnson » Blog Archive » Links: 11-13-2007

  2. Pingback: Aaron Johnson » Blog Archive » Java, Commons HTTP Client and HTTP proxies

  3. Pingback: Links: 11-14-2007 :: the correct shade of blue

  4. so with this piece of code, i could integrate it with an application? and it would automatically go through the proxy? as long as i had access to the http proxy service?

  5. HI
    I am getting following error kindly help me Server returned HTTP response code: 407 for URL:
    at com.aswinanand.sms.SMS.send(
    at MeraSMS.main(

  6. Hi
    I am also getting same problem ,any one kindly help to solve the below issue Server returned HTTP response code: 407 for URL:
    at com.aswinanand.sms.SMS.send(
    at MeraSMS.main(

  7. Several errors here.

    1. The proxySet property has never existed in the JDK, so setting it to ‘true’ or ‘false’ or ‘fred’ does nothing. It was part of the now very defunct HotJavabean (1997) and leaked into various books.

    2. The http.proxyUser and http.proxyPassword properties aren’t part of the JDK either. I believe they are part of one of the Apache libraries.

  8. We have a automatic proxy configuration file “***.pac” file.

    There is no user name or password,

    Does anyone know how to code for this situation?

  9. Hi I am running a standalone program as proxy server and provided all properties, but it is accepting though the user name is wrong.

    * Copyright (C)2011 Deutsche Bank AG APHO
    * %created_by: Vijender D (devvije) %
    * %derived_by: %
    * %date_created: 3:29:17 PM May 12, 2011 %
    * %date_modified: %
    * %state: %
    * %full_filespec: %
    * %release: %
    * Author: Vijender D (devvije)
    * Created on: May 12, 2011
    * Purpose:
    * Change History:
    * Date Author Reason
    * ————————————————————————–
    * May 12, 2011 Vijender D
    * @author devvije


    public class SimpleProxyServer {
    public static void main(String[] args) throws IOException {
    try {
    String localhost = “”;
    String remotehost = “”;
    int remoteport = 6666;
    int localport = 6666;
    // Print a start-up message
    System.out.println(“Starting proxy for ” + localhost + “:” + remoteport
    + ” on port ” + localport);

    System.getProperties().put(“http.proxyHost”, localhost);
    System.getProperties().put(“http.proxyPort”, localport);
    System.getProperties().put(“http.proxyUser”, “vijender”);
    System.getProperties().put(“http.proxyPassword”, “vijender123”);
    System.getProperties().put(“proxySet”, “true”);
    System.getProperties().put(“http.proxySet”, “true”);

    // And start running the server
    runServer(remotehost, remoteport, localport); // never returns
    } catch (Exception e) {

    * runs a single-threaded proxy server on
    * the specified local port. It never returns.
    public static void runServer(String remotehost, int remoteport, int localport)
    throws IOException {
    // Create a ServerSocket to listen for connections with
    ServerSocket ss = new ServerSocket(localport);

    final byte[] request = new byte[1024];
    byte[] reply = new byte[4096];

    while (true) {
    Socket client = null, server = null;
    try {
    // Wait for a connection on the local port
    client = ss.accept();

    final InputStream streamFromClient = client.getInputStream();
    final OutputStream streamToClient = client.getOutputStream();

    // Make a connection to the real server.
    // If we cannot connect to the server, send an error to the
    // client, disconnect, and continue waiting for connections.
    try {
    server = new Socket(remotehost, remoteport);
    } catch (IOException e) {
    PrintWriter out = new PrintWriter(streamToClient);
    out.print(“Proxy server cannot connect to ” + remotehost + “:”
    + remoteport + “:\n” + e + “\n”);

    // Get server streams.
    final InputStream streamFromServer = server.getInputStream();
    final OutputStream streamToServer = server.getOutputStream();

    // a thread to read the client’s requests and pass them
    // to the server. A separate thread for asynchronous.
    Thread t = new Thread() {
    public void run() {
    int bytesRead;
    try {
    while ((bytesRead = != -1) {
    streamToServer.write(request, 0, bytesRead);
    } catch (IOException e) {

    // the client closed the connection to us, so close our
    // connection to the server.
    try {
    } catch (IOException e) {

    // Start the client-to-server request thread running

    // Read the server’s responses
    // and pass them back to the client.
    int bytesRead;
    try {
    while ((bytesRead = != -1) {
    streamToClient.write(reply, 0, bytesRead);
    } catch (IOException e) {

    // The server closed its connection to us, so we close our
    // connection to our client.
    } catch (IOException e) {
    } finally {
    try {
    if (server != null)
    if (client != null)
    } catch (IOException e) {

  10. Hiya very nice site!! Guy .. Excellent .. Amazing .. I’ll bookmark your site and take the feeds additionally?I’m glad to search out numerous useful information right here within the post, we’d like work out more techniques in this regard, thanks for sharing. . . . . .

  11. Pingback: jvm http proxy – wiloon

  12. There is NO SUCH THING as http.proxyUser and http.proxyPassword! They don’t exist. Proxies with authentication will not work with these commands.

  13. Excellent post. I was checking continuously this blog and I am impressed!
    Very helpful info particularly the last part :
    ) I care for such info a lot. I was seeking this particular information for a long time.
    Thank you and good luck.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s