Java 如何将文件夹变成jar - 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18288141/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:05:02  来源:igfitidea点击:

How to turn folder into jar - Code

javajar

提问by marko5049

Hi im busy on a application that decompiles a jar the pastes files into the folder of the decompiled jar, it then compresses the folder into a jar.

嗨,我正忙于反编译 jar 的应用程序,将粘贴的文件粘贴到反编译的 jar 的文件夹中,然后将文件夹压缩到 jar 中。

Decompiling and copying works, but i can't manage to get the folders contents to be jared (compressed into jar), i did about 3hrs research and found only outdated methods. please help.

反编译和复制工作,但我无法设法让文件夹内容被压缩(压缩到 jar 中),我做了大约 3 小时的研究,发现只有过时的方法。请帮忙。

-Regards marko5049

- 问候 marko5049

EDIT MORE INFO: I apologize i mean i cant get my application to turn a folder into a jar file, my application is an modification installer for a jar file. and it extracts the jars files, then adds the modification and then, is supposed to then turn the folder back into a jar file so that the modification is installed. The jar file is not executable.

编辑更多信息:我很抱歉,我的意思是我无法让我的应用程序将文件夹转换为 jar 文件,我的应用程序是一个 jar 文件的修改安装程序。它提取 jars 文件,然后添加修改,然后应该将文件夹转回 jar 文件,以便安装修改。jar 文件不可执行。

回答by Jason C

Given that you want to create the JAR through code; you can use JarOutputStreamfor that. There is an example at this linkthat contains code to create a JAR file given a File[]containing all the input files.

鉴于您想通过代码创建 JAR;你可以用JarOutputStream它。此链接中有一个示例其中包含在给定File[]包含所有输入文件的情况下创建 JAR 文件的代码。

As for creating the list of files given a starting input path, see Recursively list files in Java.

关于在给定起始输入路径的情况下创建文件列表,请参阅在 Java 中递归列出文件

You could either build a list of files then just use code like in the above example, or you could recursively scan files and add them to the JAR as you go.

您可以构建一个文件列表,然后使用上面示例中的代码,或者您可以递归扫描文件并将它们添加到 JAR 中。

If you are using Java 7 and you know your users are too you can also use Files.walkFileTree()with a FileVisitorthat adds entries to the JAR as it visits files.

如果您使用的是 Java 7 并且您知道您的用户也是如此,您还可以使用Files.walkFileTree()withFileVisitor来在 JAR 访问文件时将条目添加到 JAR。



Original answer before OP clarified:OP澄清之前的原始答案:

Is there something wrong with:

是不是有什么问题:

jar cf my-application.jar folder1 folder2 folder3 etc

The JDK comes with a jarutility to create JAR files.

JDK 附带了一个jar用于创建 JAR 文件的实用程序。

You can read an official tutorial on it here: Creating JAR Files. It is very straightforward.

您可以在此处阅读有关它的官方教程:创建 JAR 文件。这是非常简单的。

If you want to create a runnableJAR, you can create a manifest file that has the main class and other options in it. The linked tutorial describes that process.

如果要创建可运行的JAR,可以创建一个清单文件,其中包含主类和其他选项。链接的教程描述了该过程。

回答by VijayVishnu

for windows just make the folder as winrar file.,

对于 Windows,只需将文件夹设为 winrar 文件即可。,

  • to do this right click the folder and click "7 -zip" then
  • choose "add to foldername.zip".
  • now a rar file is created with the same folder name.
  • Then open the cmd in current folder directory
  • type "mv foldername.zip foldername.jar"
  • 为此,请右键单击该文件夹,然后单击“7 -zip”
  • 选择“添加到文件夹名称.zip”。
  • 现在创建了一个具有相同文件夹名称的 rar 文件。
  • 然后打开当前文件夹目录下的cmd
  • 输入“mv 文件夹名.zip 文件夹名.jar”

Now you got the executable jar file with your corresponding folder.

现在您获得了带有相应文件夹的可执行 jar 文件。

回答by Shahid Hussain Abbasi

The short answer is, ZIP the folder, then rename it to a JAR file.

简短的回答是,压缩文件夹,然后将其重命名为 JAR 文件。

回答by Sugoi Reed

This worked for me for a MAC OSX:

对于 MAC OSX,这对我有用:

Open Terminal at the folder with the jar file and run the following commands

在 jar 文件所在的文件夹中打开终端并运行以下命令

 unzip mylib.jar -d jarfolder 

 //You can then change whatever you need and finally run the command below

 jar cvf mylib.jar -C jarfolder/ .

回答by learner

I just found this question and its answers are more useful for your problem: how to zip a folder itself using java

我刚刚发现这个问题,它的答案对您的问题更有用: 如何使用 java 压缩文件夹本身

2 tips :

2个提示:

1、a jar is exactly a zip. So, you just need to zip your folder, and rename it to jar

1、罐子就是一个拉链。所以,你只需要压缩你的文件夹,并将其重命名为 jar

2、be careful that you should zip your whole folder without changing the relative path of the files, but not just extract all the .class files and zip them together. Because when you run the jar, the class package should be consistent with its path.

2、注意你应该在不改变文件相对路径的情况下压缩整个文件夹,而不是将所有的.class文件解压缩在一起。因为运行jar的时候,class包要和它的路径一致。

回答by devman

Following command worked for me in Windows 10 and jdk-8u212

以下命令在 Windows 10 和 jdk-8u212 中对我有用

jar cf my-application.jar folder1 folder2 folder3 etc

回答by anna anna

You can put your files in a zip folder. Then convert the zip file into Jar format.A .jar extension file is a Java Archive format file. It is used to store a large amount of files into one single file. You can try a free online file converter without downloading a new software on your computer. There are various online file converters available on Google. I would recommend Convert zip to jar

您可以将文件放在 zip 文件夹中。然后将 zip 文件转换为 Jar 格式。.jar 扩展文件是 Java Archive 格式的文件。它用于将大量文件存储到一个文件中。您可以试用免费的在线文件转换器,而无需在您的计算机上下载新软件。Google 上提供了各种在线文件转换器。我建议将 zip 转换为 jar

I hope it helps.

我希望它有帮助。

回答by Rahul Gupta

The easiest way to make it .. put your folder to C:\Program Files\Java\jdk1.8.0_221\binand then reach till the same path from CMD then run this

制作它的最简单方法.. 将您的文件夹放入C:\Program Files\Java\jdk1.8.0_221\bin然后到达 CMD 的相同路径,然后运行它

jar cvf Name_your_jar.jar folder

jar cvf Name_your_jar.jar folder

回答by Codemaster123456789

I suggest trying to create a regular .ZIP file in Windows. You need to get 7-zip in order to view the .JAR file you are creating. You should just paste contents into the .ZIP, then rename the file type from .ZIP to .JAR, this worked for me and I hope this works for you.

我建议尝试在 Windows 中创建一个常规的 .ZIP 文件。您需要获得 7-zip 才能查看您正在创建的 .JAR 文件。您应该将内容粘贴到 .ZIP 中,然后将文件类型从 .ZIP 重命名为 .JAR,这对我有用,我希望这对您有用。

.JARs are basically .ZIPs created by the Oracle Java client, so you need special file viewing software such as 7-zip or WinRAR to view it for some reason.

.JAR 基本上是由 Oracle Java 客户端创建的 .ZIP,因此出于某种原因,您需要特殊的文件查看软件(例如 7-zip 或 WinRAR)才能查看它。

You can also revert .JARs to .ZIPs by renaming the file type. You might have to mod your computer with RegeEdit or something to have access to renaming your file types.

您还可以通过重命名文件类型将 .JAR 恢复为 .ZIP。您可能必须使用 RegeEdit 或其他工具修改您的计算机才能重命名您的文件类型。

I hope this helps.

我希望这有帮助。