我正在使用当前代码,该代码成功下载并运行来自互联网的jar文件:
package Joehot200;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
public class Main {
String version = "0.4";
boolean needDownload = false;
public void needDownload(){
StringBuilder page = new StringBuilder();
URL google;
try {
google = new URL("http://www.endcraft.net/ver");
//URL google = new URL("www.endcraft.net/Post");
URLConnection yc = google.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine + " || " + version);
if (!inputLine.contains(version)){
needDownload = true;
System.out.println("Downloading!");
}else{
System.out.println("No downloaded needed!");
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Main() {
needDownload();
if (needDownload){
try {
URL url = new URL("http://www.endcraft.net/webstart/privateers.jar");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("HEAD");
download("http://www.endcraft.net/webstart/privateers.jar", "privateers.jar", conn.getContentLength());
} catch (Exception e) {
e.printStackTrace();
}
try {
String exec = (System.getProperty("user.dir") + "\\privateers.jar");
String[] command = {"javaw", "-jar", exec};
final Process process = Runtime.getRuntime().exec(command);
System.out.println("Running " + exec);
System.exit(0);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
void download(String source, String destination, int size) {
// ten percent of the total download size
File ofile = new File(System.getProperty("user.dir") + "", destination);
System.out.printf("\nDownloading\n\t%s\nTo\n\t%s\n", source, destination);
try {
if (ofile.exists()) ofile.delete();
if (!ofile.createNewFile()) {
throw new IOException("Can't create " + ofile.getAbsolutePath());
}
int inChar = 0;
URL url = new URL(source);
InputStream input = url.openStream();
FileOutputStream fos = new FileOutputStream(ofile);
for (int i = 0; i < size && inChar != -1; i++) {
inChar = input.read();
fos.write(inChar);
}
input.close();
fos.close();
System.out.println("Downloaded " + ofile.getAbsolutePath());
} catch (EOFException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Main frame = new Main();
}
}
但是,出于某种原因,它会一直打开我多次下载的应用程序,即使我打开它后立即调用system.exit(0)!
另外,我无法终止此过程。如果我点击&#34;终止&#34;在eclipse中,该过程不会终止并保持打开的东西。即使在关闭Eclipse之后,它仍然在运行并打开窗口。 我至少可以和任务经理一起杀掉它,但这对游戏来说并不是很好。
我该如何解决这个问题?我的代码中的错误在哪里?
谢谢!