在运行时期间提取jar库

时间:2017-09-23 05:49:47

标签: java jar

我创建了一个名为JarUnpacker的类,我想在我的主程序的第一个程序中使用它来解压由另一个开发人员添加的.jar库。 但它提取了一个无用的文件。有人可以帮助我,我该怎么办? 这是我的代码:

public class JarUnpacker {

  public void setFile(InputStream io, String fileName) throws IOException {
    new File("lib").mkdir();
    FileOutputStream fos = new FileOutputStream(fileName);
    byte[] buf = new byte[256];
    int read = 0;
    while ((read = io.read(buf)) > 0) {
      fos.write(buf, 0, read);
    }
  }

  public void unpack() {
    InputStream input = getClass().getResourceAsStream("lib/TobeUnpacked.jar");
    try {
      this.setFile(input,"lib/TobeUnpacked");
    }
    catch(IOException e) {
      System.out.println("failed");
      e.printStackTrace(System.out);
    }
  }

  public static void main(String args[]) {
    JarUnpacker j = new JarUnpacker();
    j.unpack();
  } 

}

0 个答案:

没有答案
相关问题