简体   繁体   English

无法创建JAR文件

[英]Trouble Creating JAR File

I've been trying for hours to turn the program I wrote into a JAR file, none of the examples I've found seem to be working. 我已经花了好几个小时把我写的程序变成了一个JAR文件,我发现的所有例子似乎都没有。 I use JCreator to write my code, and I'm not particularly familiar with using the command prompt. 我使用JCreator编写代码,我对使用命令提示符并不是特别熟悉。 If anyone can try to walk me through making a JAR file, I'm sure I could ask questions and create one. 如果有人可以试着指导我制作一个JAR文件,我相信我可以提出问题并创建一个。

In Java, it is common to combine several classes in one .jar ("java archive") file. 在Java中,通常在一个.jar(“java archive”)文件中组合多个类。 Library classes are stored that way. 库类以这种方式存储。 Larger projects (such as the Case Study in the AP program) use jar files. 较大的项目(例如AP程序中的案例研究)使用jar文件。 You can create your own jar file combining several classes, too. 您也可以创建自己的jar文件,组合几个类。

jar files are created using the jar.exe utility program from JDK. jar文件是使用JDK的jar.exe实用程序创建的。 You can make your jar file runnable by telling jar.exe which class has main. 您可以通过告诉jar.exe哪个类具有main来使您的jar文件可运行。 To do that, you need to create a manifest file. 为此,您需要创建清单文件。 A manifest is a one-line text file with a "Main-Class" directive. 清单是带有“Main-Class”指令的单行文本文件。 For example: 例如:

Main-Class: Craps 主类:胡扯

This line must end with a newline. 此行必须以换行符结尾。

A jar file created with a main class manifest can be used both as a library and a runnable jar. 使用主类清单创建的jar文件既可以用作库,也可以用作可运行的jar。 If you use it as a library, you can edit and compile any of the classes included in the jar, and add it to your project. 如果将其用作库,则可以编辑和编译jar中包含的任何类,并将其添加到项目中。 Then it will override the one in the jar file. 然后它将覆盖jar文件中的那个。

You can create a manifest file in any text editor, or even by using the MS-DOS echo command. 您可以在任何文本编辑器中创建清单文件,甚至可以使用MS-DOS echo命令。 You can give your manifest file any name, but it's better to use something standard, such as manifest.txt. 您可以为清单文件指定任何名称,但最好使用标准的东西,例如manifest.txt。

Once you have a manifest and all your classes have been compiled, you need to run JDK's jar.exe utility. 一旦有了清单并且所有类都已编译完毕,就需要运行JDK的jar.exe实用程序。 It is located in the JDK's bin folder, the same place where javac.exe and java.exe are. 它位于JDK的bin文件夹中,与javac.exe和java.exe所在的位置相同。 jar.exe takes command-line arguments; jar.exe接受命令行参数; if you run it without any arguments, it will display the usage information and examples. 如果你在没有任何参数的情况下运行它,它将显示用法信息和示例。 You need 你需要

C\\mywork> jar cvfm MyJarName.jar manifest.txt *.class C \\ mywork> jar cvfm MyJarName.jar manifest.txt * .class

cvfm means "create a jar; show verbose output; specify the output jar file name; specify the manifest file name." cvfm表示“创建一个jar;显示详细输出;指定输出jar文件名;指定清单文件名。” This is followed by the name you wish to give to your jar file, the name of your manifest file, and the list of .class files that you want included in the jar. 接下来是您希望提供给jar文件的名称,清单文件的名称以及要包含在jar中的.class文件列表。 *.class means all class files in the current directory. * .class表示当前目录中的所有类文件。

Actually, if your manifest contains only the Main-Class directive, you can specify the main class directly on the jar.exe's command line, using the e switch, instead of m. 实际上,如果清单只包含Main-Class指令,则可以使用e开关直接在jar.exe的命令行上指定主类,而不是m。 Then you do not need a separate manifest file; 那你就不需要一个单独的清单文件了; jar will add the required manifest to your jar file for you. jar会为您的jar文件添加所需的清单。 For example: 例如:

C\\mywork> jar cvfe MyJarName.jar MyMainClass *.class C \\ mywork> jar cvfe MyJainName.jar MyMainClass * .class

Below is a reference for creating a jar file in the detailed steps for doing this in Command Prompt and in JCreator. 下面是在命令提示符和JCreator中执行此操作的详细步骤中创建jar文件的参考。

Creating a jar File in JCreator 在JCreator中创建jar文件

You can configure a "tool" that will automate the jar creation process. 您可以配置一个“工具”来自动化jar创建过程。 You only need to do it once. 你只需要做一次。

  1. Click on Configure/Options. 单击“配置/选项”。
  2. Click on Tools in the left column. 单击左列中的“工具”。
  3. Click New, and choose Create Jar file. 单击New,然后选择Create Jar file。
  4. Click on the newly created entry Create Jar File in the left column under Tools. 单击Tools下左侧列中新创建的条目Create Jar File。
  5. Edit the middle line labeled Arguments: it should have 编辑标记为Arguments的中间行:它应该有

    cvfm $[PrjName].jar manifest.txt *.class

  6. Click OK. 单击确定。

Creating a jar File in Command Prompt 在命令提示符中创建jar文件

  1. Start Command Prompt. 启动命令提示符。
  2. Navigate to the folder that holds your class files: 导航到包含类文件的文件夹:

    C:\\>cd \\mywork

  3. Set path to include JDK's bin. 设置路径以包含JDK的bin。 For example: 例如:

    C:\\mywork> path c:\\Program Files\\Java\\jdk1.7.0_25\\bin;%path%

  4. Compile your class(es): 编译你的班级:

    C:\\mywork> javac *.java

  5. Create a manifest file and your jar file: 创建清单文件和jar文件:

    C:\\mywork> echo Main-Class: Craps >manifest.txt
    C:\\mywork> jar cvfm Craps.jar manifest.txt *.class
    or 要么
    C:\\mywork> jar cvfe Craps.jar Craps *.class

  6. Test your jar: 测试你的罐子:

    C:\\mywork> Craps.jar or C:\\mywork> java -jar Craps.jar C:\\mywork> Craps.jarC:\\mywork> java -jar Craps.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM