Configure HTTP/HTTPS Proxy Settings Java

For local networks inside an organisation it is common to access the public internet through a HTTP Proxy. This tutorial will show you how you can globally set the HTTP Proxy Settings in Java.

Use system Proxy Settings

If you have a proxy configured on your local system. You can try to set the (default is false) this property will try to apply the system properties.

This property is introduced since JDK 1.5. Note that this property is checked only once at startup.
System.setProperty("", "true");

Using Command Line JVM Settings

You can configure your server to use a HTTP Proxy server with JVM command line arguments. You can pass the following arguments either when you use a http or https proxy server.



Passing arguments to Tomcat

Append following properties to the file in Tomcat: ${CATALINA_HOME}/conf/



Append following properties to the startup file in Tomcat:

  • ${CATALINA_HOME}/bin/catalina.bat for windows
  • ${CATALINA_HOME}/bin/ for linux/mac
JAVA_OPTS="-Dhttp.proxyHost= -Dhttp.proxyPort=80"

Programatically set HTTP Proxy Settings

If you want you can also add proxy settings programmatically use the same properties but this time set them using System.setProperty();.

System.setProperty("http.proxyHost", "");
System.setProperty("http.proxyPort", "80");
System.setProperty("http.nonProxyHosts", "localhost|");

System.setProperty("https.proxyHost", "");
System.setProperty("https.proxyPort", "443");

Authenticating Proxy

Setting http.proxyUser and http.proxyPassword will not automatically authenticate via a proxy.

Taking the above into account. Here is how you can Authenticate via a proxy. This initialisation code is typically executed at application startup. So make sure you register this authenticator before you make any HTTP Requests that require Proxy Authentication.

// settings proxy credentials
System.setProperty("http.proxyUser", "proxyUser");
System.setProperty("http.proxyPassword", "secret");

// Java ignores http.proxyUser. Here come's the workaround.
Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        if (getRequestorType() == RequestorType.PROXY) {
            String prot = getRequestingProtocol().toLowerCase();
            String host = System.getProperty(prot + ".proxyHost", "");
            String port = System.getProperty(prot + ".proxyPort", "80");
            String user = System.getProperty(prot + ".proxyUser", "");
            String password = System.getProperty(prot + ".proxyPassword", "");
            if (getRequestingHost().equalsIgnoreCase(host)) {
                if (Integer.parseInt(port) == getRequestingPort()) {
                    return new PasswordAuthentication(user, password.toCharArray());
        return null;


You may also like...