如何使用 Java 将文件移动到已编译的 .jar 文件中? [英] How do I move a file into a compiled .jar file using Java?

查看:49
本文介绍了如何使用 Java 将文件移动到已编译的 .jar 文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个文件导入系统,但我无法将文件移动到应用程序所在的已编译 .jar 文件中.这是我正在尝试做的:

I'm making a file import system, and I can't move files into the compiled .jar file the application is in. Here's what I'm trying to do:

Path FROM = Paths.get(filePath.getText());
Path TO = Paths.get("C:\\Users\\" + System.getProperty("user.name") + 
"\\AppData\\Roaming\\.minecraft\\mods\\music_crafter-1.0\\src\\main\\resources\\assets\\music_crafter\\sounds\\block\\music_player");
                                       //jar file
Files.move(FROM, TO.resolve(FROM.getFileName()), StandardCopyOption.REPLACE_EXISTING);

推荐答案

您需要在内部处理 jar 文件.Jar 不是一个目录,它是一个压缩的容器文件(几乎是一个具有不同扩展名的 ZIP 文件).

You need to handle the jar file internally. A Jar is not a directory, it is a compressed container file (pretty much a ZIP file with a different extension).

为此,鉴于您使用的是 Java 6,您有 2 个选择:

To do this, given that you are on Java 6, you have 2 options:

  1. 将内容解压到一个临时工作目录(有内置在 API 中,或者使用诸如 Apache Commons Compress 之类的库)完成您的工作(复制、删除等),然后重新压缩.
  2. 对附带的 Jar 实用程序进行外部命令行调用爪哇
  1. Unzip the contents to a temporary working directory (there are built in APIs for this, or use a library such as Apache Commons Compress) do your work (copying, deleting, etc) and then re-zip.
  2. Make external command line calls to the Jar utilities that come with Java

其中,只有 (1) 个真正有意义.

Of those, only (1) makes any real sense.

如果您可以将 Java 升级到 7+,则可以使用第三个选项,即:3. 使用 Zip 文件系统提供程序 在代码中将其视为文件系统

A third option would be available if you could up your Java to 7+ which would be: 3. Use a Zip File System Provider to to treat it as a file system in code

尽管如此:

根据对您的问题的评论,您真的可能想看看这是否是您需要做的事情?为什么需要插入现有的罐子?如果这是外部"数据,那么最好放在单独的资源位置/容器中,而不是应用程序 jar 中.

As per comments on your question, you really might want to look at if this something you need to do at all? Why do you need to insert into existing jars? If this is 'external' data, it would be much better in a separate resource location/container, not the application jar.

这篇关于如何使用 Java 将文件移动到已编译的 .jar 文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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