Configuring proxy in Java applications
Tuesday, April 29th, 2008If you have a Java application that needs to connect to Internet, and you are behind a proxy, you have two options:
- Add this to the application command line parameters:
-Dhttp.proxyHost=<host.of.proxy> -Dhttp.proxyPort=<port.of.proxy>
- Or edit the net.properties file in the lib folder of your java home. It is self-explanatory, since you have to edit one or two lines and there’s about 70 lines of comments on that file.
The bad news is if your proxy requires authentication. This means you will have to do something like:
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username",
new char[] { 49, 50, 51, 52, 53, 54 } );
}
});
You can, of course, create a more complex Authenticator subclass, that will open a Popup asking for the proxy user and password.