简体   繁体   English

在Java中将文件添加到现有的jar文件

[英]Adding files to an existing jar file in java

I need some help with using the following code to modify and existing jar file: 使用以下代码修改现有的jar文件时,我需要一些帮助:

    String command = "cmd /c jar uf " + dirToModify + " " + Main.getMain().outputLocate.getSelectedFile();
    try {
        Runtime.getRuntime().exec(command);
    } catch (IOException e) {
        e.printStackTrace();
    }

    dirToModify = "C:\\Users\\Me\\Desktop\\myfile.jar"
    Main.getMain().outputLocate.getSelectedFile() = "C:\\Users\\Me\\Desktop\\myfolder"

Basically I want to add the files/folders from myfolder to myfile.jar but with the above code it will add a shortcut to my C: drive not the files from myfolder . 基本上,我想将文件/文件夹从myfolder添加到myfile.jar但是使用上述代码,它将为我的C:驱动器添加快捷方式,而不是myfolder的文件。

Also I did look at other posts but none help me with this problem. 我也确实看过其他帖子,但是没有人帮助我解决这个问题。

Any help with this would be greatly appreciated. 任何帮助,将不胜感激。

I'd suggest, first checking, if adding one file, at a time, is working with this code. 我建议,首先检查是否一次添加一个文件是否正在使用此代码。 This will make clear if problem is in 'folder addition' or 'file addition'. 如果“文件夹添加”或“文件添加”存在问题,这将使您清楚。 If file addition doesn't work, your basic jar update logic is broken. 如果添加文件无效,则您的基本jar更新逻辑已损坏。 So you can ask for solution to that problem. 因此,您可以请求该问题的解决方案。 If file addition works, try recursively adding all files from destination folder. 如果添加文件有效,请尝试递归添加目标文件夹中的所有文件。

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

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