Friday, July 22, 2005

Proxy setting for Java programs

Quite often whenever we need to connect to the internet thru the company firewall, we need to set the proxy details for the Java interpreter.

Here is the syntax:
java -DproxySet=true -DproxyHost=localhost -DproxyPort=80

In Ant, we need to set it as:

set ANT_OPTS=-DproxySet=true -DproxyHost=localhost -DproxyPort=80
ant mytarget

Alternately, your application can specify proxy settings before using a URLConnection :

// Modify system properties
Properties sysProperties = System.getProperties();
// Specify proxy settings
sysProperties.put("proxyHost", "myhost");
sysProperties.put("proxyPort", "myport");
sysProperties.put("proxySet", "true");