Many times, a Java app needs to connect to the Internet. The most common example happens when it is reading an XML file and needs to download its schema.
I am behind a proxy server. How do I set my JVM to use the proxy ?
Many times, a Java app needs to connect to the Internet. The most common example happens when it is reading an XML file and needs to download its schema.
I am behind a proxy server. How do I set my JVM to use the proxy ?
You can set those flags programmatically this way:
Just return the right values from the methods
needsProxy()
,getProxyHost()
andgetProxyPort()
and you can call this code snippet whenever you want.Recently I've discovered the way to allow JVM to use browser proxy settings. What you need to do is to add
${java.home}/lib/deploy.jar
to your project and to init the library like the following:Afterwards the proxy settings are available to Java API via
java.net.ProxySelector
.The only problem with this approach is that you need to start JVM with
deploy.jar
in bootclasspath e.g.java -Xbootclasspath/a:"%JAVA_HOME%\jre\lib\deploy.jar" -jar my.jar
. If somebody knows how to overcome this limitation, let me know.You can set some properties about the proxy server as jvm parameters
-Dhttp.proxyPort=8080, proxyHost, etc.
but if you need pass through an authenticating proxy, you need an authenticator like this example:
ProxyAuthenticator.java
Example.java
Based on this reply: http://mail-archives.apache.org/mod_mbox/jakarta-jmeter-user/200208.mbox/%3C494FD350388AD511A9DD00025530F33102F1DC2C@MMSX006%3E
This is a minor update, but since Java 7, proxy connections can now be created programmatically rather than through system properties. This may be useful if:
Here's a contrived example in groovy:
Full Reference: http://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html
As is pointed out in other answers, if you need to use Authenticated proxies, there's no reliable way to do this purely using command-line variables - which is annoying if you're using someone else's application and don't want to mess with the source code.
Will Iverson makes the helpful suggestion over at Using HttpProxy to connect to a host with preemtive authentication to use a Proxy-management tool such as Proxifier ( http://www.proxifier.com/ for Mac OS X and Windows) to handle this.
For example with Proxifier you can set it up to only intercept java commands to be managed and redirected through its (authenticated) proxy. You're going to want to set the proxyHost and proxyPort values to blank in this case though, e.g. pass in
-Dhttp.proxyHost= -Dhttp.proxyPort=
to your java commands.I think configuring WINHTTP will also work.
Many programs including Windows Updates are having problems behind proxy. By setting up WINHTTP will always fix this kind of problems