Java jar的清单属性 [英] Manifest attribute for java jar

查看:96
本文介绍了Java jar的清单属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有了您的所有帮助,我得以用Java完成我的第一个项目.现在,我想创建一个jar并运行jar中的应用程序(Java项目-这是一个普通的控制台应用程序,它具有依赖于另一个项目(控制台应用程序)).

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.

我通过右键单击创建了一个带有日食的罐子-导出-创建了一个罐子.当我尝试从cmd运行此jar时,出现错误(以下是我得到的错误)

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)

no main manifest attribute, in AAA.jar

我用Google搜索了错误-大多数错误都创建了清单文件.我在项目中创建了一个类似于src级别的清单文件

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

Manifest-Version: 1.0
Main-Class: com.Pacakename.mainclass
Class-Path: log4j-1.2.16.jar db2jcc.jar db2jcc_license_cu.jar

然后J尝试再次运行jar,但这一次它说在类中没有main方法的情况下没有main方法

Then J tried run the jar again but this time it says no main method where i have a main method in the class

请有人解释创建清单的一个明确步骤(如果您向我展示拥有清单文件的地方的文件夹结构,对我有帮助)

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)

推荐答案

让我们假设您具有以下目录结构:

Lets assume you have following directory structure:

MyJavaProject
   |-src 
      |- com
          |- example
               |- Main.java

要在cmd行中编译此类项目(无外部依赖项),您需要调用命令

To compile such project in cmd line, (no external dependencies) you need to invoke commands

$ cd MyJavaProject
$ mkdir bin         //to separate *.class file from source files
$ javac -d bin src\com\example\Main.java

这将在bin目录中创建Main.class文件.要将其打包到* .jar文件,您可以: 1)用二进制文件创建jar并在cmd中指定Main class 2)创建Manifes并将其嵌入到jar中(我将重点介绍这一点)

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)

您应该在src下创建META-INF目录,并在其中创建MANIFEST.mf文件

You should create META-INF directory under src and inside it create MANIFEST.mf file

您的清单应如下所示:

Manifest-Version: 1.0
Created-By: <Your info>
Main-Class: com.example.Main

记住要在清单末尾添加空行!

在这种情况下,您指定Manifest-Version属性,Created-By属性以及要在Main-Class属性中运行的主类的完全限定名称

In this case, you specify Manifest-Version attribute, Created-By attribute, and fully qualified name of main class to run in Main-Class attribute

要使用此清单文件和您的二进制文件创建Jar,请调用命令

To create Jar with this Manifest file and you binary file, invoke commands

$ cd bin
$ jar cfm MyJavaProject.jar ..\src\META-INF\MANIFEST.MF .

这将创建一个名为MyJavaProject.jar的新jar,并使用您的清单

this will create new jar called MyJavaProject.jar and use your manifest

如果您的项目依赖于外部类或jar,则在编译时将它们添加到类路径中(-cp选项),并在Manifest

If you project depends on external classas or jar, add them to your classpath when compiling (-cp option) and add another line in Manifest

ClassPath: path/to/dependent/jars/jar.jar

重新编译并创建新的jar并享受您的Java任务:)

Recompile it and create new jar and enjoy your Java quest :)

有关清单的更多信息,请参见:文档

For more info on Manifest please see: docs

PS:使用jars时,从cmd行进行放大似乎很丑陋,但是它可以教给您一些类似于ava的概念.如果您想跳过它,请考虑使用 Apache Maven Apache Ant

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 Maven or Apache Ant

这篇关于Java jar的清单属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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