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