Java 修改 jar 中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1224817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Modifying a file inside a jar
提问by Hamza Yerlikaya
I would like to modify a file inside my jar. Is it possible to do this without extracting and re jarring, from within my application?
我想修改我的 jar 中的一个文件。是否可以在我的应用程序中在不提取和重新震动的情况下执行此操作?
File i want to modify are configuration files, mostly xml based.
我要修改的文件是配置文件,主要是基于 xml 的。
The reason i am interested in not un jarring is that the application is wrapped with launch4j if i unjar it i can't create the .exe file again.
我感兴趣的原因是,如果我解压它,应用程序会用 launch4j 包装,我将无法再次创建 .exe 文件。
采纳答案by clwhisk
You can use the u
option for jar
您可以使用该u
选项jar
From the Java Tutorials:
来自 Java 教程:
jar uf jar-file input-file(s)
"Any files already in the archive having the same pathname as a file being added will be overwritten."
“存档中已有的与添加的文件具有相同路径名的任何文件都将被覆盖。”
See Updating a JAR File.
请参阅更新 JAR 文件。
Much better than making the whole jar all over again. Invoking this from within your program sounds possible too. Try Running Command Line in Java
比重新制作整个罐子要好得多。从您的程序中调用它听起来也是可能的。尝试在 Java 中运行命令行
回答by Sorantis
As long as this file isn't .class, i.e. resource file or manifest file - you can.
只要此文件不是 .class,即资源文件或清单文件 - 您就可以。
回答by JustJeff
Java jar files are the same format as zip files - so if you have a zip file utility that would let you modify an archive, you have your foot in the door. Second problem is, if you want to recompile a class or something, you probably will just have to re-build the jar; but a text file or something (xml, for instance) should be easily enough modified.
Java jar 文件的格式与 zip 文件的格式相同 - 因此,如果您有一个 zip 文件实用程序可以让您修改存档,那么您就大功告成了。第二个问题是,如果你想重新编译一个类或其他东西,你可能只需要重新构建 jar;但是文本文件或其他东西(例如xml)应该很容易修改。
回答by aperkins
To expand on what dfa said, the reason is because the jar file is set up like a zip file. If you want to modify the file, you must read out all of the entries, modify the one you want to change, and then write the entries back into the jar file. I have had to do this before, and that was the only way I could find to do it.
扩展一下 dfa 所说的,原因是因为 jar 文件设置为 zip 文件。如果要修改文件,则必须读出所有条目,修改要更改的条目,然后将条目写回 jar 文件。我以前不得不这样做,这是我能找到的唯一方法。
EDIT
编辑
Note that this is using the internal to Java jar file editors, which are file streams. I am sure there is a way to do it, you could read the entire jar into memory, modify everything, then write back out to a file stream. That is what I believe utilities like 7-Zip and others are doing, as I believe the ToC of a zip header has to be defined at write time. However, I could be wrong.
请注意,这是使用 Java jar 文件编辑器的内部文件流。我确信有一种方法可以做到,您可以将整个 jar 读入内存,修改所有内容,然后写回文件流。这就是我认为 7-Zip 等实用程序正在做的事情,因为我认为必须在写入时定义 zip 标头的 ToC。但是,我可能是错的。
回答by dimo414
This may be more work than you're looking to deal with in the short term, but I suspect in the long term it would be very beneficial for you to look into using Ant(or Maven, or even Bazel) instead of building jar's manually. That way you can just click on the ant file (if you use Eclipse) and rebuild the jar.
这可能比您在短期内要处理的工作更多,但我怀疑从长远来看,考虑使用Ant(或Maven,甚至Bazel)而不是手动构建 jar对您来说非常有益. 这样,您只需单击 ant 文件(如果您使用 Eclipse)并重建 jar。
Alternatively, you may want to actually not have these config files in the jar at all - if you're expecting to need to replace these files regularly, or if it's supposed to be distributed to multiple parties, the config file should not be part of the jar at all.
或者,您可能希望在 jar 中实际上根本没有这些配置文件 - 如果您希望定期替换这些文件,或者如果它应该分发给多方,则配置文件不应该是罐子。
回答by ZZ Coder
As many have said, you can't change a file in a JAR without recanning the JAR. It's even worse with Launch4J, you have to rebuild the EXE once you change the JAR. So don't go this route.
正如许多人所说,您不能在不重新扫描 JAR 的情况下更改 JAR 中的文件。Launch4J 的情况更糟,一旦你改变了 JAR,你必须重建 EXE。所以不要走这条路。
It's generally bad idea to put configuration files in the JAR. Here is my suggestion. Search for your configuration file in some pre-determined locations (like home directory, \Program Files\ etc). If you find a configuration file, use it. Otherwise, use the one in the JAR as fallback. If you do this, you just need to write the configuration file in the pre-determined location and the program will pick it up.
将配置文件放在 JAR 中通常是个坏主意。这是我的建议。在某些预先确定的位置(如主目录、\Program Files\ 等)搜索您的配置文件。如果您找到配置文件,请使用它。否则,使用 JAR 中的那个作为后备。如果你这样做,你只需要在预先确定的位置写入配置文件,程序就会把它捡起来。
Another benefit of this approach is that the modified configuration file doesn't get overwritten if you upgrade your software.
这种方法的另一个好处是,如果您升级软件,修改后的配置文件不会被覆盖。
回答by shoaib
Yes you can, using SQLite you can read from or write to a database from within the jar file, so that you won't have to extract and then re jar it, follow my post http://shoaibinamdar.in/blog/?p=313
是的,你可以,使用 SQLite 你可以从 jar 文件中读取或写入数据库,这样你就不必提取然后重新 jar 它,按照我的帖子http://shoaibinamdar.in/blog/? p=313
using the syntax "jdbc:sqlite::resource:" you would be able to read and write to a database from within the jar file
使用语法“jdbc:sqlite::resource:”,您将能够从 jar 文件中读取和写入数据库
回答by GreenGiant
回答by Tomasz Mularczyk
Extract jar file for ex. with winrar and use CAVAJ:
为ex提取jar文件。使用 winrar 并使用 CAVAJ:
Cavaj Java Decompiler is a graphical freeware utility that reconstructs Java source code from CLASS files.
Cavaj Java Decompiler 是一个图形化的免费软件实用程序,它可以从 CLASS 文件中重建 Java 源代码。
here is video tutorial if you need: https://www.youtube.com/watch?v=ByLUeem7680
如果您需要,这里是视频教程:https: //www.youtube.com/watch?v=ByLUeem7680
回答by Buvin Perera
Not sure if this help, but you can edit without extracting:
不确定这是否有帮助,但您可以在不提取的情况下进行编辑:
- Open the jar file from vi editor
- Select the file you want to edit from the list
- Press enter to open the file do the changers and save it pretty simple
- 从 vi 编辑器打开 jar 文件
- 从列表中选择要编辑的文件
- 按回车键打开文件做转换器并保存它非常简单
Check the blog post for more details http://vinurip.blogspot.com/2015/04/how-to-edit-contents-of-jar-file-on-mac.html
查看博客文章了解更多详情 http://vinurip.blogspot.com/2015/04/how-to-edit-contents-of-jar-file-on-mac.html