How do I set the proxy for the JVM?

Many Java applications need Internet access, such as when reading an XML file that requires downloading its schema. Since I am behind a proxy server, how can I configure my JVM to use the java http.proxy settings to ensure proper connectivity?

The simplest way to set a proxy for your JVM is by passing system properties when running your Java application.

java -D http.proxyHost=proxy.example.com -Dhttp.proxyPort=8080 -D https.proxyHost=proxy.example.com -D https.proxyPort= 8443 -jar myApp.jar

:heavy_check_mark: Pros: Quick, no code changes required.

:x: Cons: Must be specified every time you run the application.

If you need to set the proxy dynamically within your Java code, you can use:

  • System.setProperty(“http.proxyHost”, “proxy.example.com”);
  • System.setProperty(“http.proxyPort”, “8080”);
  • System.setProperty(“https.proxyHost”, “proxy.example.com”);
  • System.setProperty(“https.proxyPort”, “8443”);

:heavy_check_mark: Pros: Works even if the app is launched without proxy settings.

:x: Cons: Requires modifying the application code.

If you need fine-grained control, such as bypassing the proxy for specific hosts, use Java’s Proxy class:

  • Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(“proxy.example.com”, 8080));
  • URL url = new URL(“http://example.com”);
  • HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);

:heavy_check_mark: Pros: Allows selective proxying (e.g., some connections can bypass the proxy).

:x: Cons: More complex, requires modifying the networking logic.

Setting Proxy for the JVM

There are several ways to configure proxy settings for the JVM:

1. Using System Properties (most common method)

You can set these properties when launching your Java application:

bash

Copy

Download

java -Dhttp.proxyHost=proxy.momoproxy.com -Dhttp.proxyPort=8080 -Dhttps.proxyHost=proxy.momoproxy.com -Dhttps.proxyPort=8080 YourApplication

Or for authenticated proxies:

bash

Copy

Download

java -Dhttp.proxyHost=proxy.momoproxy.com -Dhttp.proxyPort=8080 -Dhttp.proxyUser=username -Dhttp.proxyPassword=password YourApplication

2. Setting in code

You can configure the proxy programmatically:

java

Copy

Download

System.setProperty(“http.proxyHost”, “proxy.momoproxy.com”); System.setProperty(“http.proxyPort”, “8080”); System.setProperty(“https.proxyHost”, “proxy.momoproxy.com”); System.setProperty(“https.proxyPort”, “8080”); // For authenticated proxies Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(“username”, “password”.toCharArray()); } });

3. Common Proxy Properties

  • http.proxyHost - Hostname of the proxy server
  • http.proxyPort - Port of the proxy server (default is 80)
  • https.proxyHost - Proxy for HTTPS connections
  • https.proxyPort - HTTPS proxy port (default is 443)
  • http.nonProxyHosts - Hosts that should bypass proxy (e.g., “localhost|127.0.0.1|*.example.com”)
  • http.proxyUser - Username for proxy authentication
  • http.proxyPassword - Password for proxy authentication

4. For Specific Protocols

Some applications may need to set protocol-specific proxies:

bash

Copy

Download

java -DsocksProxyHost=proxy.momoproxy.com -DsocksProxyPort=1080 YourApplication

Notes

  1. These settings affect all HTTP/HTTPS connections made by the JVM
  2. For security reasons, avoid hardcoding credentials in your code
  3. Some applications/libraries might have their own proxy configuration that overrides these settings
  4. For containerized applications, you may need to set these in your container configuration

Would you like more specific information about any of these methods?

1 Like