java jar 的清单属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17096587/
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
Manifest attribute for java jar
提问by tom
With all the help from you i was able to finish my first project in java. Now i want to create a jar and run the application (Java project - it is a plain console application which is having another project(console application) as a dependence) from jar.
在你们的帮助下,我完成了我的第一个 Java 项目。现在我想创建一个 jar 并从 jar 运行应用程序(Java 项目 - 它是一个普通的控制台应用程序,它有另一个项目(控制台应用程序)作为依赖项)。
I created a jar with eclipse by right click - export - created a jar. When i try to run this jar from my cmd, I have an error (below is the error I am geting)
我通过右键单击使用 Eclipse 创建了一个 jar - 导出 - 创建了一个 jar。当我尝试从我的 cmd 运行这个 jar 时,我有一个错误(下面是我得到的错误)
no main manifest attribute, in AAA.jar
I Googled the error - most of them subjected to create the Manifest file. I created a manifest file like below in the project equal to src level
我在谷歌上搜索了错误 - 其中大多数都受到创建清单文件的影响。我在等于 src 级别的项目中创建了一个如下所示的清单文件
Manifest-Version: 1.0
Main-Class: com.Pacakename.mainclass
Class-Path: log4j-1.2.16.jar db2jcc.jar db2jcc_license_cu.jar
Then J tried run the jar again but this time it says no main method where i have a main method in the class
然后 J 尝试再次运行 jar,但这次它说没有 main 方法,我在类中有一个 main 方法
Please some one please explain a clear step's for creating the Manifest(it really help's me if you show me the folder structure of the place where we have Manifest file)
请有人解释创建清单的明确步骤(如果您向我展示我们拥有清单文件的位置的文件夹结构,这对我真的很有帮助)
回答by Filip Zymek
Lets assume you have following directory structure:
假设您有以下目录结构:
MyJavaProject
|-src
|- com
|- example
|- Main.java
To compile such project in cmd line, (no external dependencies) you need to invoke commands
要在 cmd 行中编译此类项目,(无外部依赖项)您需要调用命令
$ cd MyJavaProject
$ mkdir bin //to separate *.class file from source files
$ javac -d bin src\com\example\Main.java
This will create Main.class
file in bin
directory. To pack it to *.jar file, you can:
1) create jar with binary files and specify Main class in cmd
2) create Manifes and embbed it to jar (I will focus on this one)
这将Main.class
在bin
目录中创建文件。打包成*.jar文件,你可以: 1)用二进制文件创建jar并在cmd中指定Main类 2)创建Manifes并将其嵌入到jar中(我将重点关注这个)
You should create META-INF
directory under src
and inside it create MANIFEST.mf
file
你应该在它META-INF
下面src
和里面创建目录创建MANIFEST.mf
文件
Your manifest should look like this:
您的清单应如下所示:
Manifest-Version: 1.0
Created-By: <Your info>
Main-Class: com.example.Main
Remember to add empty line at end on Manifest!!
记得在 Manifest 的末尾添加空行!!
In this case, you specify Manifest-Version
attribute, Created-By
attribute, and fully qualified name of main class to run in Main-Class
attribute
在这种情况下,您指定要在属性中运行的主类的Manifest-Version
属性、Created-By
属性和完全限定名称Main-Class
To create Jar with this Manifest file and you binary file, invoke commands
要使用此清单文件和二进制文件创建 Jar,请调用命令
$ cd bin
$ jar cfm MyJavaProject.jar ..\src\META-INF\MANIFEST.MF .
this will create new jar called MyJavaProject.jar
and use your manifest
这将创建新的 jarMyJavaProject.jar
并使用您的清单
If you project depends on external classas or jar, add them to your classpath when compiling (-cp
option) and add another line in Manifest
如果您的项目依赖于外部类或 jar,请在编译时将它们添加到您的类路径中(-cp
选项)并在其中添加另一行Manifest
ClassPath: path/to/dependent/jars/jar.jar
Recompile it and create new jar and enjoy your Java quest :)
重新编译它并创建新的 jar 并享受你的 Java 任务:)
For more info on Manifest please see: docs
有关 Manifest 的更多信息,请参阅:docs
PS: Working with jars,amnifest from cmd line might seem ugly, but it can teach you some ava-like concepts. If you want to skip it, please consider using Apache Mavenor Apache Ant
PS:使用 jars,来自 cmd 行的 amnifest 可能看起来很丑陋,但它可以教你一些类似 ava 的概念。如果你想跳过它,请考虑使用Apache Maven或Apache Ant
回答by tbsalling
If you are using mavenI can recommend you to use the shade pluginto produce runnable jars with all dependencies (and manifest:-)) on-board.
如果您使用的是maven,我可以建议您使用shade 插件来生成具有所有依赖项(和清单:-))的可运行 jars 。
Include this in your pom.xml:
将此包含在您的pom.xml 中:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!-- your main class: -->
<mainClass>org.sonatype.haven.HavenCli</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
And then execute mvn clean package shade:shade
to build a jar that you can run from the command line with java -jar jarfilename.jar
.
然后执行mvn clean package shade:shade
以构建一个 jar,您可以使用java -jar jarfilename.jar
.
回答by mschenk74
If you are using eclipse you can use "Export" --> Runnable jar to achieve your goals. In that case you should have a valid run configuration for your project since eclipse uses this run configuration as a kind of blueprint for the runnable jar.
如果您使用的是 eclipse,则可以使用“导出”--> Runnable jar 来实现您的目标。在这种情况下,您的项目应该有一个有效的运行配置,因为 eclipse 使用此运行配置作为可运行 jar 的一种蓝图。
In this case eclipse will build the manifest for you.
在这种情况下,eclipse 将为您构建清单。