You should use a command similar to: java -DsocksProxyHost=localhost -DsocksProxyPort=5000 ….standardStuffHere…
With this, the JVM will try to connect to a socks proxy running on
port 5000 of localhost, and do every network access inside the entire
JVM using it. No changes of any kind in your application is
necessary - you just need to add those two JVM parameters.
This is very powerful in combination with the ssh socks support. It
means that you can, for instance, test an application that needs
access to multiple resources (database, queue server, etc, etc) from
your home, as long as you have ssh access to a machine inside the
corporate network.
You can also use this together with reverse SSH proxies (see above) to
run a applications on a cloud computing service such as EC2 or
Rackspace cloud without needing to drill permanent holes in your
firewall setup. This is very useful - imagine you had a spike on
demand, and urgently would like to use some cloud service to put more
instances of whatever you are running up. However, much of your stuff
depends on other services, which run inside your network and are
firewalled/under NAT/etc/etc/etc.
In the past I did stuff similar to
ssh -N -f -D4000 igorhvr@localhost # This inside some machine in the main datacenter I was using. I now have a SOCKS server running on port 4000.
ssh -R2000:localhost:4000 -N -f igorhvr@machine-in-some-cloud-service # Now the port 2000 there will end up proxies to my local SOCKS server.
ssh igorhvr@machine-in-some-cloud-service java
-DsocksProxyHost=localhost -DsocksProxyPort=2000 my.app.MainClass
the end result: I have my application running in the cloud service,
but behaving exactly like it would in my main datacenter. This
includes access to databases, queue services, and pretty much anything
you could need.
As a last note, you might want your JVM to tunnel everything through
the SOCKS proxy, except traffic for a specific machine. You can add
exceptions using the -DsocksNonProxyHosts:155.119.100.255 flag.
Thus you can use something similar to: java -DsocksProxyHost=localhost
-DsocksProxyPort=5000 -DsocksNonProxyHosts:155.119.100.255
….standardStuffHere…
And when your application is running every network access will happen
through the SOCKS proxy, except for anything directed to the
155.119.100.255 IP.