简体   繁体   English

如何从Java执行Java?

[英]How do I Execute Java from Java?

I have this DownloadFile.java and downloads the file as it should: 我有这个DownloadFile.java并按需下载文件:

import java.io.*;
import java.net.URL;

public class DownloadFile {    
    public static void main(String[] args) throws IOException {
        String fileName = "setup.exe";
            // The file that will be saved on your computer
        URL link = new URL("http://onlinebackup.elgiganten.se/software/elgiganten/setup.exe");
            // The file that you want to download

        // Code to 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 to execute this from a mouse event in Gui.java . 我想从Gui.java的鼠标事件执行此Gui.java

private void jLabel17MouseClicked(java.awt.event.MouseEvent evt){

}   

How do I do this? 我该怎么做呢?

Your current method is a static method, which is fine, but all the data that it extracts is held tightly within the main method, preventing other classes from using it, but fortunately this can be corrected. 您当前的方法是静态方法,可以,但是它提取的所有数据都紧紧地保存在main方法中,从而防止了其他类使用它,但是幸运的是,可以对此进行更正。

My suggestion: 我的建议:

  • re-write your DownloadFile code so that it is does not simply a static main method, but rather a method that can be called by other classes easily, and that returns the data from the file of interest. 重新编写您的DownloadFile代码,这样它不仅是静态的main方法,而且可以被其他类轻松调用,并且可以从目标文件中返回数据。 This way outside classes can call the method and then receive the data that the method extracted. 这样,外部类可以调用该方法,然后接收该方法提取的数据。
  • Give it a String parameter that will allow the calling code to pass in the URL address. 给它一个String参数,它将允许调用代码传递URL地址。
  • Give it a File parameter for the file that it should write data to. 给它一个文件参数,该文件应向其写入数据。
  • Consider having it return data (a byte array?), if this data will be needed by the calling program. 如果调用程序需要此数据,请考虑使其返回数据(字节数组?)。
  • Or if it does not need to return data, perhaps it could return boolean to indicate if the download was successful or not. 或者,如果不需要返回数据,则可能返回布尔值以指示下载是否成功。
  • Make sure that your method throws all exceptions (such as IO and URL excptions) that it needs to throw. 确保您的方法抛出了需要抛出的所有异常(例如IO和URL异常)。
  • Also, if this is to be called by a Swing GUI, be sure to call this type of code in a background thread, such as in a SwingWorker, so that this code does not tie up the Swing event thread, rendering your GUI frozen for a time. 另外,如果要由Swing GUI调用此代码,请确保在后台线程(例如在SwingWorker中)调用此类型的代码,以使该代码不会占用Swing事件线程,从而导致GUI冻结一个时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM