从正在运行的jar应用程序内部复制jar文件失败 [英] failed copy jar file from inside running jar application

查看:76
本文介绍了从正在运行的jar应用程序内部复制jar文件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从正在运行的jar中复制jar文件,可以在eclipse IDE中运行,但是在外部运行jar应用程序时不能.这里是部分代码:

I try to copy jar file from within running jar, it was okay running inside eclipse IDE, but NOT when running jar application outside. Here part of the codes :

AWdir = new File("C:\\Windows\\Temp\\aw\\");
    AWdir.mkdir();
    if(AWdir!=null && !AWdir.isDirectory()){
        MessageDialog.openError(getShell(), "Error create directry", "DIR:"+AWdir);
    }
    String resource = "generate.jar";
    URL res = MainWindow.this.getClass().getResource(resource);
    fileJar = new File(res.getFile());
    JarFile jarFile=new JarFile(fileJar);
    String fileName = jarFile.getName();
    String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
    File destFile = new File(AWdir, fileNameLastPart);

    JarOutputStream jos = new JarOutputStream(new FileOutputStream(destFile));
    Enumeration<JarEntry> entries = jarFile.entries();

    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        InputStream is = jarFile.getInputStream(entry);
        //jos.putNextEntry(entry);
        //create a new entry to avoid ZipException: invalid entry compressed size
        jos.putNextEntry(new JarEntry(entry.getName()));
        byte[] buffer = new byte[4096];
        int bytesRead = 0;
        while ((bytesRead = is.read(buffer)) != -1) {
            jos.write(buffer, 0, bytesRead);
        }
        is.close();
        jos.flush();
        jos.closeEntry();
    }
    jos.close();
    destFile.createNewFile();

请提供帮助,并感谢您提出解决上述问题的建议

please help and thank you for recommendation to resolve the above problem

推荐答案

使用简单的代码即可解决:

it is solved using simple code like :

AWdir = new File("C:\\Windows\\Temp\\aw\\");
    AWdir.mkdir();
    if(AWdir!=null && !AWdir.isDirectory()){
        MessageDialog.openError(getShell(), "Error create directry", "DIR:"+AWdir);
    }
    String resource = "generate.jar";
    URL res = MainWindow.this.getClass().getResource(resource);   
//replace this line
    //fileJar = new File(res.getFile());
// become here
    fileJar = new File(AWdir.getAbsolutePath()+"\\"+"generate.jar");
//finally write down this
    FileUtils.copyURLToFile(res, fileJar);

我从>如何在内部复制文件罐子放在罐子外面?非常感谢.

这篇关于从正在运行的jar应用程序内部复制jar文件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆