title: Trying to download an exe file from a website and run it
body: <p>I am having trouble downloading an exe file from a server and running it. specifically downloading git and installing it automatically. I found some code on another question that isn't quite working, and I don't know why. The parameters I pass it  are:</p>
  
  <pre><code>URL url = new URL("http://git-scm.com/download/win");
  String fileName = "C:/SETUP/gitinstall.exe";
  </code></pre>
  
  <p>Am thinking maybe it is something wrong with the link I am opening in the URL.
  Any help you can give me would be greatly appreciated. </p>
  
  <p>To try and be clear: It gets a file into the directory but when I try to launch gitinstall.exe, I get an error from windows that the file isn't compatible with my version of windows. </p>
  
  <p>However if I download the link through chrome it runs fine. Also The file it downloads is only 8 kb while the file downloaded through chrome is around 15 mb if that helps. Thank you again.</p>
  
  <pre><code>public static void saveFile(URL url, String file) throws IOException {
      System.out.println("opening connection");
      InputStream in = url.openStream();
      FileOutputStream fos = new FileOutputStream(new File(file));
  
      System.out.println("reading file...");
      int length = -1;
      byte[] buffer = new byte[1024];// buffer for portion of data from
      // connection
      while ((length = in.read(buffer)) &gt; -1) {
          fos.write(buffer, 0, length);
      }
      fos.close();
      in.close();
      System.out.println("file was downloaded");
  }
  </code></pre>