简体   繁体   English

Jar 忽略所有方法

[英]Jar ignoring all methods

I was making a pretty simple jar to unzip a zip and run the jar that was inside of it.我正在制作一个非常简单的 jar 来解压缩一个 zip 并运行它里面的 jar。 The problem I've run into is that it doesn't do anything at all.我遇到的问题是它根本没有做任何事情。

This is the main, and only class file for the jar.这是 jar 的主要且唯一的类文件。 The manifest does point correctly to it, and it loads without errors.清单确实正确指向它,并且加载时没有错误。

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static java.lang.Integer.parseInt;
import java.net.URLConnection;
import java.net.URL;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.Enumeration;
import sign.signlink;
import java.nio.file.*;
import java.io.FileReader;

    public class ClientUpdater {

    private String fileToExtractNew = "/client.zip";

    private String getJarDir() throws FileNotFoundException, IOException{
            String linebuf="",verStr="";
            FileInputStream fis = new FileInputStream("/runLocationURL.txt");
            BufferedReader br= new BufferedReader(new InputStreamReader(fis));
            while ((linebuf = br.readLine()) != null) {
                verStr = linebuf;
            }
            return verStr;
    }

    public static void main(String[] args) {
System.out.println("start");
    }

    private void unZip() {
    System.out.println("unzipping");
        try {
            ZipEntry zipEntry;
            //client
            BufferedInputStream bufferedInputStreamNew = new BufferedInputStream(new FileInputStream(this.fileToExtractNew));
            ZipInputStream zipInputStreamNew = new ZipInputStream(bufferedInputStreamNew);

            //client
            while ((zipEntry = zipInputStreamNew.getNextEntry()) != null) {
                String stringNew = zipEntry.getName();
                File fileNew = new File(this.getJarDir() + File.separator + stringNew);
                if (zipEntry.isDirectory()) {
                    new File(this.getJarDir() + zipEntry.getName()).mkdirs();
                    continue;
                }
                if (zipEntry.getName().equals(this.fileToExtractNew)) {
                    this.unzipNew(zipInputStreamNew, this.fileToExtractNew);
                    break;
                }
                new File(fileNew.getParent()).mkdirs();
                this.unzipNew(zipInputStreamNew, this.getJarDir() + zipEntry.getName());
            }
            zipInputStreamNew.close();
        }
        catch (Exception var1_2) {
            var1_2.printStackTrace();
        }
    }

    private void unzipNew(ZipInputStream zipInputStreamNew, String stringNew) throws IOException {
    System.out.println("unzipping new");
        FileOutputStream fileOutputStreamNew = new FileOutputStream(stringNew);
        byte[] arrby = new byte[4024];
        int n = 0;
        while ((n = zipInputStreamNew.read(arrby)) != -1) {
            fileOutputStreamNew.write(arrby, 0, n);
        }
        fileOutputStreamNew.close();
        Runtime.getRuntime().exec("java -jar " + getJarDir() + "/Project Pk Client.jar");
        System.exit(0);
    }
}

It shows the "Start" message, but not the other 2, so it never reaches those methods.它显示“开始”消息,但不显示其他 2 个消息,因此它永远不会到达这些方法。 Is it because they aren't being called?是因为他们没有被召唤吗? I'm still learning Java.我还在学习Java。

You actually have to call your other methods from main .您实际上必须从main调用其他方法。 Right now, all you are telling the computer to do is print start and then exit.现在,您告诉计算机要做的就是打印start然后退出。 Functions do not get called simply by existing.函数不会简单地被现有调用。

It seems based on a quick glance that you just need to add unzip();乍一看,您似乎只需要添加unzip(); to your main function, right after the System.out.println line.到您的main函数,就在System.out.println行之后。

To do this, you need to say that those other methods are static , so you need to say private static void unZip() instead of private void unZip() .为此,您需要说那些其他方法是static ,因此您需要说private static void unZip()而不是private void unZip() Do this for your other methods too.对您的其他方法也这样做。

import java.io.*;
import static java.lang.Integer.parseInt;
import java.net.URLConnection;
import java.net.URL;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.Enumeration;
import sign.signlink;
import java.nio.file.*;

public class ClientUpdater {

    private String fileToExtractNew = "/client.zip";

    private static String getJarDir() throws FileNotFoundException, IOException{
        String linebuf="",verStr="";
        FileInputStream fis = new FileInputStream("/runLocationURL.txt");
        BufferedReader br= new BufferedReader(new InputStreamReader(fis));
        while ((linebuf = br.readLine()) != null) {
            verStr = linebuf;
        }
        return verStr;
    }

    public static void main(String[] args) {
        System.out.println("start");
        unZip();
    }

    private static void unZip() {
        System.out.println("unzipping");
        try {
            ZipEntry zipEntry;
            //client
            BufferedInputStream bufferedInputStreamNew = new BufferedInputStream(new FileInputStream(this.fileToExtractNew));
            ZipInputStream zipInputStreamNew = new ZipInputStream(bufferedInputStreamNew);

            //client
            while ((zipEntry = zipInputStreamNew.getNextEntry()) != null) {
                String stringNew = zipEntry.getName();
                File fileNew = new File(this.getJarDir() + File.separator + stringNew);
                if (zipEntry.isDirectory()) {
                    new File(this.getJarDir() + zipEntry.getName()).mkdirs();
                    continue;
                }
                if (zipEntry.getName().equals(this.fileToExtractNew)) {
                    this.unzipNew(zipInputStreamNew, this.fileToExtractNew);
                    break;
                }
                new File(fileNew.getParent()).mkdirs();
                this.unzipNew(zipInputStreamNew, this.getJarDir() + zipEntry.getName());
            }
            zipInputStreamNew.close();
        }
        catch (Exception var1_2) {
            var1_2.printStackTrace();
        }
    }

    private static void unzipNew(ZipInputStream zipInputStreamNew, String stringNew) throws IOException {
        System.out.println("unzipping new");
        FileOutputStream fileOutputStreamNew = new FileOutputStream(stringNew);
        byte[] arrby = new byte[4024];
        int n = 0;
        while ((n = zipInputStreamNew.read(arrby)) != -1) {
            fileOutputStreamNew.write(arrby, 0, n);
        }
        fileOutputStreamNew.close();
        Runtime.getRuntime().exec("java -jar " + getJarDir() + "/Project Pk Client.jar");
        System.exit(0);
    }
}

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

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