Saturday 6 July 2013

Open browser with proxy settings set from Java code

      In this post we aim to to write a java application which lets the user click a button to open a browser with its proxy settings set. To open an application from java we can use the exec method of the java Runtime class. So one can simply open a browser from java code. But the second part of the problem states that its proxy settings must be set to a specific value. In order to achieve this we shall make use of Google Chrome's switches. A detailed description of the entire process is given below.

Google Chrome Switches : 

      The Chrome Browser allows the use of command line flags (or "switches") that the browser accepts in order to enable particular features or modify otherwise default functionality. To use these switches we need to modify the target of an application. This can be done as follows.

Step 1 : Create a shortcut of the chrome.exe file. (To create a shortcut right click on the chrome.exe application, go to the Send To submenu and click on Desktop(create shortcut) item. This will create a shortcut of the application on your desktop).

Step 2 : Right click on the shortcut and click on Properties.

Step 3 : Append the following flag to the target text box value --proxy-server=127.0.0.1:8080. Here 127.0.0.1, 8080 are the IP and port of the proxy server (You may change these to reflect your own proxy server). With this flag the target must now look like chrome.exe --proxy-server=127.0.0.1:8080. Note that there is a space between chrome.exe and the flag.

Step 4 : Click on Apply and close the Properties window.

      Now  if you open the shortcut it will start the browser with its proxy setting set. Note that if an instance of the chrome browser is already opened then the proxy settings wont be used, so you must close any running instances of the browser before opening the shortcut. These proxy settings will not be shown in the proxy settings accessible through the settings tab of chrome. So in order to verify you can set the proxy to any random free ip-port address and open the shortcut. If the proxy settings were indeed set, opening any website in the browser will show you the following error : Unable to connect to the proxy server. (for this check you may use 127.0.0.1:8080 as proxy settings provided you do not have a proxy set up on your machine).

      So if we are able to somehow open this shortcut from our java code we are set. We shall do exactly this with the exec method of the  Runtime class.


The Runtime class :

      The Runtime Class in java encapsulates the runtime environment. You can get a reference to the current Runtime object by calling the Runtime.getRuntime() method. Once a reference to the current runtime object is obtained we can call the exec method to execute a specific command. This command must open the shortcut of the browser that we had created earlier. The following command accomplishes this  :

cmd /c start /d \"d:\" chrome.lnk

      Note that here d: is the path of where our application is and chrome.lnk is the name of the application.  This means that I had kept the shortcut in my d: drive. So you can change the path to reflect the location of your shortcut file. (The name is chrome .lnk because the shortcuts in windows have the extension lnk).


The CODE!

Following is the complete code that does the above :

public class OpenBrowser {
   public static void main(String[] args) {
      try {
         Process process = Runtime.getRuntime().exec("cmd /c start /d \"d:\" chrome.lnk");
      }
      catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}


Now the incorporation of the above method into an application where a click on a button triggers the above task is trivial.

Warning : Note that Google states the following on its chromium page
"It is important to note that using these switches is not supported or recommended. They should only be used for temporary cases and may break in the future."
So use this solution at your own risk :)
  

No comments:

Post a Comment