class - How do I Execute Java from Java? -


i have downloadfile.java , downloads file should:

import java.io.*; import java.net.url;  public class downloadfile {         public static void main(string[] args) throws ioexception {         string filename = "setup.exe";             // file saved on computer         url link = new url("http://onlinebackup.elgiganten.se/software/elgiganten/setup.exe");             // file want download          // code download         inputstream in = new bufferedinputstream(link.openstream());         bytearrayoutputstream out = new bytearrayoutputstream();         byte[] buf = new byte[1024];         int n = 0;         while (-1 != (n = in.read(buf))) {             out.write(buf, 0, n);         }         out.close();         in.close();         byte[] response = out.tobytearray();          fileoutputstream fos = new fileoutputstream(filename);         fos.write(response);         fos.close();         // end download code          system.out.println("finished");     } } 

i want execute mouse event in gui.java.

private void jlabel17mouseclicked(java.awt.event.mouseevent evt){  }    

how do this?

your current method static method, fine, data extracts held tightly within main method, preventing other classes using it, fortunately can corrected.

my suggestion:

  • re-write downloadfile code not static main method, rather method can called other classes easily, , returns data file of interest. way outside classes can call method , receive data method extracted.
  • give string parameter allow calling code pass in url address.
  • give file parameter file should write data to.
  • consider having return data (a byte array?), if data needed calling program.
  • or if not need return data, perhaps return boolean indicate if download successful or not.
  • make sure method throws exceptions (such io , url excptions) needs throw.
  • also, if called swing gui, sure call type of code in background thread, such in swingworker, code not tie swing event thread, rendering gui frozen time.

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -