自动更新程序无法正常工作

时间:2012-01-21 16:44:57

标签: java

所以我试图为我在java中制作的游戏制作一个自动更新程序,但它不起作用。通过大量调试,我发现它是尝试启动游戏时停止的部分。它没有任何意义。无论如何它都无法与你合作,因为我正在运行我的“网站”的IISC服务器是私有的,仅用于测试。源:

Downloader.java:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;


public class Downloader {
    public static void DownloadFile(String Indir,String Outdir,String Inname,String Outname) throws MalformedURLException, IOException{
        InputStream Input = new URL(Indir+"/"+Inname).openStream();
        if(new File(Outdir+"/"+Outname).exists()){
            new File(Outdir+"/"+Outname).delete();
        }
        File f = new File(Outdir);
        f.mkdirs();
        OutputStream Output = new FileOutputStream(Outdir+"/"+Outname);
        Scanner scan = new Scanner(Input);
        while(scan.hasNext()){
            byte[] bytes = scan.nextLine().getBytes();
            Output.write(bytes, 0, bytes.length);
        }
        Input.close();
        Output.close();
    }
}

Launcher.java:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import javax.swing.JOptionPane;



public class Launcher{
    private static final String dir = "http://tv-w7:8000/";

    public static void main(String[] args){ 
        new Launcher(args);
    }
    public Launcher(String[] args){
        if(args.length==1){if(args[0].equals("f")){
            try {
                    JOptionPane.showMessageDialog(null, "game is getting cleaned, press OK to proseed");
                    DownloadNewVersion(false,false);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }else{
            try {

                Download();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void Download() throws MalformedURLException, IOException{
        if(new File(System.getenv("APPDATA")+"/MS2-torsteinv/MS2-bin/no/torsteinv/MarsSettlement2/Client/Client.class").exists()){
            InputStream AVIS = new URL(dir+"MS2-dat/CD.txt").openStream();
            InputStream CVIS = new FileInputStream(System.getenv("APPDATA")+"/MS2-torsteinv/MS2-dat/CD.txt");

            Scanner CVS = new Scanner(CVIS);
            Scanner AVS = new Scanner(AVIS);

            String CV = CVS.nextLine();
            String AV = AVS.nextLine();

            CVIS.close();
            AVIS.close();

            if(!CV.equals(AV))DownloadNewVersion(true,true);
            else start();
        }else{
            DownloadNewVersion(true,false);
        }
    }
    private void DownloadNewVersion(boolean promt,boolean N) throws IOException {
        if(promt && N)if(JOptionPane.showConfirmDialog(null,"A new verson is avalible. Download?")==1);
        InputStream OS = Runtime.getRuntime().exec("java -classpath "+System.getenv("APPDATA")+"\\MS2-torsteinv").getInputStream();
        Scanner s = new Scanner(OS);
        while(s.hasNext())System.out.println(s.nextLine());
        Scanner scan = new Scanner(new URL(dir+"MS2-register.txt").openStream());
        String CL = "";
        while(scan.hasNext()){
            CL = scan.nextLine();
            Downloader.DownloadFile(dir+CL.split(":")[0],System.getenv("APPDATA")+"/MS2-torsteinv/"+CL.split(":")[0],CL.split(":")[1],CL.split(":")[1]);
        }
        start();
    }
    private void start(){

        try {
            InputStream OS = Runtime.getRuntime().exec(new String[]{"java ",System.getenv("APPDATA")+"\\MS2-torsteinv\\MS2-bin\\no\\torsteinv\\MarsSettlement2\\Client\\Client.class"}).getErrorStream();
            Scanner s = new Scanner(OS);
            while(s.hasNext())System.out.println(s.nextLine());
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.exit(0);
    }
}

我最终得到了一个错误:

java.lang.ClassFormatError: Illegal UTF8 string in constant pool in class file Client
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Exception in thread "main" 

2 个答案:

答案 0 :(得分:0)

这可能是因为您将二进制流视为包含使用您的平台默认编码编码的字符:

byte[] bytes = scan.nextLine().getBytes();

这认为输入流包含文本行,将字节转换为字符(解码:可能有损,因为所有字节序列都不代表有效字符),然后将字符转换为字节。它还忘记编写可能在二进制流中的换行符分隔符。

了解如何在the Java IO tutorial中读取和写入二进制数据。

附注:您的代码很难阅读,因为它不符合Java命名约定。

答案 1 :(得分:0)

你的exec调用的结尾有“Client.class”。那应该只是“客户”吗?

试试这个:

InputStream OS = Runtime.getRuntime().exec(new String[]{"java", "-classpath", System.getenv("APPDATA")+"\\MS2-torsteinv\\MS2-bin\\no\\torsteinv\\MarsSettlement2\\Client", "Client"}).getErrorStream();