Java 如何创建具有包结构的jar文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18146361/
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
How to create jar file with package structure?
提问by Anderson
I have a folder structures
我有一个文件夹结构
/com/cdy/ws/a.class files
/com/cdy/ws/b.class files
/com/cdy/ws/c.class files
When I run the following command “jar cvf asd.jar *.class” it gives jar with all the class files. But the folder structure is not getting generated. All the class files have to be under “com.cdy/ws” but all the classes are in same level of META-INF. Can anyone tell me what is the command to generate the package structure?
当我运行以下命令“jar cvf asd.jar *.class”时,它会提供包含所有类文件的 jar。但是文件夹结构没有生成。所有的类文件都必须在“com.cdy/ws”下,但所有的类都在 META-INF 的同一级别。谁能告诉我生成包结构的命令是什么?
Thanks
谢谢
采纳答案by Adam Batkin
You need to start creating the JAR at the rootof the files.
您需要在文件的根目录下开始创建 JAR 。
So, for instance:
因此,例如:
jar cvf program.jar -C path/to/classes .
That assumes that path/to/classes
contains the com
directory.
假设path/to/classes
包含com
目录。
FYI, these days it is relatively uncommon for most people to use the jar
command directly, as they will use a build tool such as Antor Mavento take care of that (and other aspects of the build). It is well worth the effort of allowing one of those tools to take care of all aspects of your build, and it's even easier with a good IDE to help write the build.xml
(Ant) or pom.xml
(Maven).
仅供参考,现在大多数人jar
直接使用命令相对不常见,因为他们将使用诸如Ant或Maven 之类的构建工具来处理(以及构建的其他方面)。让这些工具中的一个来处理构建的所有方面是非常值得的,而且使用好的 IDE 来帮助编写build.xml
(Ant) 或pom.xml
(Maven)会更容易。
回答by Brian Agnew
You want
你要
$ jar cvf asd.jar .
to specify the directory (e.g. .
) to jar
from. That will maintain your folder structure within the jar file.
指定要从的目录(例如.
)jar
。这将在 jar 文件中维护您的文件夹结构。
回答by Daniel
From the directory containing the com
folder:
从包含com
文件夹的目录中:
$ jar cvf asd.jar com
added manifest
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/cdy/(in = 0) (out= 0)(stored 0%)
adding: com/cdy/ws/(in = 0) (out= 0)(stored 0%)
adding: com/cdy/ws/a.class(in = 0) (out= 0)(stored 0%)
adding: com/cdy/ws/b.class(in = 0) (out= 0)(stored 0%)
adding: com/cdy/ws/c.class(in = 0) (out= 0)(stored 0%)
$ jar -tf asd.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/cdy/
com/cdy/ws/
com/cdy/ws/a.class
com/cdy/ws/b.class
com/cdy/ws/c.class
回答by Shib
this bellow code gave me correct response
这个波纹管代码给了我正确的回应
jar cvf MyJar.jar *.properties lib/*.jar -C bin .
it added the (log4j) properties file, it added the jar files in lib. and then it went inside bin to retrieve the class files with package.
它添加了(log4j)属性文件,它在lib中添加了jar文件。然后它进入 bin 以检索带有 package.json 的类文件。
回答by Vinayak Dornala
Step 1: Go to directory where the classes are kept using command prompt (or Linux shell prompt)
Like for Project.C:/workspace/MyProj/bin/classess/com/test/*.class
步骤 1:使用命令提示符(或 Linux shell 提示符)
像 Project 一样转到保存类的目录。C:/workspace/MyProj/bin/classess/com/test/*.class
Go directory bin using command:
使用命令转到目录 bin:
cd C:/workspace/MyProj/bin
Step 2: Use below command to generate jar file.
第 2 步:使用以下命令生成 jar 文件。
jar cvf helloworld.jar com\test\hello\Hello.class com\test\orld\HelloWorld.class
Using the above command the classes will be placed in a jar in a directory structure.
使用上述命令,类将被放置在目录结构的 jar 中。
回答by Swati Pisal
Your specified folderName must be on C:\Program Files\Java\jdk1.7.0_02\bin
path
Foldername having class files.
您指定的 folderName 必须C:\Program Files\Java\jdk1.7.0_02\bin
位于具有类文件的路径 Foldername 上。
C:\Program Files\Java\jdk1.7.0_02\bin>jar cvf program1.jar Foldername
Now program1.jar will create in C:\Program Files\Java\jdk1.7.0_02\bin path
现在 program1.jar 将在 C:\Program Files\Java\jdk1.7.0_02\bin 路径中创建
回答by ArifMustafa
Simply more than above -
不仅仅是以上 -
Keep your Java packaging contents inside a directory and make sure there is nothing inside except your Java packages and their corresponding Java classes.
Open Command(If Windows) Prompt, reach to the containing directory path like below -
C:> cd "C:\Users\UserABC\Downloads\Current Folder"
C:\Users\UserABC\Downloads\Current Folder>
jar cvf hello-world-1.0.1.jar .
将您的 Java 打包内容保存在一个目录中,并确保除了您的 Java 包及其相应的 Java 类之外,没有任何内容。
打开命令(如果是 Windows)提示,到达包含目录路径,如下所示 -
C:> cd "C:\Users\UserABC\Downloads\Current Folder"
C:\用户\UserABC\下载\当前文件夹>
jar cvf hello-world-1.0.1.jar .
回答by loretoparisi
To avoid to add sources files .java
to your package you should do
为避免将源文件添加.java
到您的包中,您应该这样做
cd src/
jar cvf mylib.jar com/**/*.class
Supposed that your project structure was like
假设你的项目结构是这样的
myproject/
src/
com/
mycompany/
mainClass.java
mainClass.class
回答by Sayan Shankhari
This is what I do inside .sh file, let's consider install.sh
这是我在 .sh 文件中所做的,让我们考虑 install.sh
#!/bin/sh
echo Installing
cd .../Your_Project_Directory/com/cdy/ws/
jar cfe X.jar Main *.class
cd .../Your_Project_Directory/
ln -s .../Your_Project_Directory/com/cdy/ws/X.jar X
echo Testing...
java -jar X
echo You are Good to Go...Use hapily
#etc.
Creating Executable Jar file at the Class directory and creating a SymLink at anywhere you want.
在 Class 目录中创建 Executable Jar 文件并在您想要的任何位置创建一个 SymLink。
Run it using,
运行它,
$ sh install.sh[ENTER]
$ java -jar X[ENTER]
回答by shan
Assume your project folder structure as follows :
假设您的项目文件夹结构如下:
c:\test\classes\com\test\awt\Example.class
c:\test\classes\manifest.txt
You can issue following command to create a “Example.jar.
您可以发出以下命令来创建“Example.jar”。
jar -cvfm Example.jar manifest.txt com/test/awt/*.class
jar -cvfm Example.jar manifest.txt com/test/awt/*.class
For Example :
例如 :
go to folder structure from commmand prompt "cd C:\test\classes"
从命令提示符“cd C:\test\classes”转到文件夹结构
C:\test\classes>jar -cvfm Example.jar manifest.txt com/test/awt/*.class
C:\test\classes>jar -cvfm Example.jar manifest.txt com/test/awt/*.class