How do I set the proxy to be used by the JVM
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The JVM can use proxies through standard system properties, command-line flags, or programmatic proxy selection. The right approach depends on whether you want one global proxy for the whole process or fine-grained control per request or per protocol.
Use JVM System Properties For Global Proxy Settings
The most common way is to set system properties for HTTP and HTTPS:
This affects code that respects the standard JVM networking properties.
A large part of older Java networking code and many libraries pick up these values automatically, which is why they remain the default answer.
The Same Settings Can Be Passed At Startup
Instead of setting them in code, you can supply them on the command line:
This is often cleaner in production because proxy configuration stays outside the application code.
It also makes environment-specific changes easier across local development, CI, and deployment systems.
Use Authentication If The Proxy Requires It
If the proxy needs credentials, add an Authenticator:
This works with Java networking APIs that participate in the standard authentication flow.
Per-Request Proxy Control Is Different
Sometimes a global JVM proxy is too blunt. In that case, use a proxy object on the client API itself:
This is useful when different outbound calls should use different routes or when only part of the application should go through the proxy.
System Proxies And Non-Proxy Hosts
The JVM can also be told to consult operating-system proxy settings:
That can be convenient on managed desktops, but it makes behavior depend more heavily on the host environment. If you need deterministic deployment behavior, explicit JVM properties are often clearer.
Non-proxy hosts matter too. Without them, internal calls to localhost, loopback, or private domains may get routed unnecessarily through the proxy.
That becomes especially important in enterprise environments where health checks, callbacks, and internal service calls should stay on private routes instead of being pushed through a corporate proxy by accident.
When troubleshooting, print the effective system properties at startup so you know which proxy settings the process actually received. That simple check prevents a lot of confusion between code defaults, shell flags, and deployment-time overrides.
Common Pitfalls
One common mistake is setting only the HTTP proxy and forgetting HTTPS, then wondering why secure connections still bypass the proxy or fail.
Another issue is assuming every third-party library honors the JVM's standard proxy properties automatically. Many do, but not all.
A third problem is hard-coding proxy credentials in application source instead of using environment or deployment configuration.
Finally, developers sometimes use global JVM proxy settings when only one client or one service call actually needs a proxy, which makes debugging routing behavior harder.
Summary
- Use
http.proxyHost,http.proxyPort,https.proxyHost, andhttps.proxyPortfor standard JVM-wide proxy configuration. - Passing these as
-Dflags at startup is often cleaner than setting them in code. - Add
http.nonProxyHoststo keep local or internal traffic out of the proxy. - Use an
Authenticatorif proxy authentication is required. - For fine-grained routing, create per-request proxy settings instead of changing the whole JVM.

