天天看点

运行 jar 文件_如何运行JAR文件

运行 jar 文件

Today we will learn how to run JAR File in java. In last tutorial we learned how to create jar file. Here we will learn how to run jar file from command line and some other aspects of using jar file.

今天,我们将学习如何在Java中运行JAR文件。 在上一个教程中,我们学习了如何创建jar文件 。 在这里,我们将学习如何从命令行运行jar文件以及使用jar文件的其他方面。

如何运行JAR文件 (How to run JAR File)

We can run a jar file using java command but it requires

Main-Class

entry in jar manifest file.

我们可以使用java命令运行jar文件,但需要jar清单文件中的

Main-Class

条目。

Main-Class is the entry point of the jar and used by java command to execute the class. My project structure looks like below image.

Main-Class是jar的入口点,由java命令用于执行该类。 我的项目结构如下图所示。

I have to add manifest file in jar file, so I will create a manifest file with below details.

我必须在jar文件中添加清单文件,因此我将使用以下详细信息创建清单文件。

manifest.txt

manifest.txt

Main-Class: com.journaldev.util.MyUtil
           

Now we will use jar command to create jar file with our manifest file data.

现在,我们将使用jar命令使用清单文件数据创建jar文件。

[email protected]:~/CODE/MyProject/bin$ jar cvfm MyProject.jar manifest.txt com
added manifest
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/journaldev/(in = 0) (out= 0)(stored 0%)
adding: com/journaldev/test/(in = 0) (out= 0)(stored 0%)
adding: com/journaldev/test/MyTest.class(in = 444) (out= 303)(deflated 31%)
adding: com/journaldev/util/(in = 0) (out= 0)(stored 0%)
adding: com/journaldev/util/MyUtil.class(in = 444) (out= 304)(deflated 31%)
           

Now when I unpack and check the contents of MANIFEST.MF file, it contains following data.

现在,当我打开包装并检查MANIFEST.MF文件的内容时,它包含以下数据。

Manifest-Version: 1.0
Created-By: 1.6.0_37 (Apple Inc.)
Main-Class: com.journaldev.util.MyUtil
           

Manifest-Version and Created-By entries are added by jar command. Now we are ready to run jar file through command line.

Manifest-Version和Created-By条目由jar命令添加。 现在,我们准备通过命令行运行jar文件。

从命令行运行Jar文件 (Run Jar File from command line)

[email protected]:~/CODE/MyProject/bin$ java -jar MyProject.jar 
MyUtil main method
           

So it’s executed MyUtil main method. That’s great when we have single class with main method.

因此它执行了MyUtil main方法。 当我们只有一个带有main方法的类时,这很棒。

What if my project has multiple entry points and I want to change the entry point without creating the jar again. So we can use the jar command to update the manifest file.

如果我的项目有多个入口点,并且我想更改入口点而不再次创建jar,该怎么办。 因此,我们可以使用jar命令来更新清单文件。

For updating a file using jar command, file structure should be similar otherwise it will add new file to another directory. Since manifest file is located at

META-INF/MANIFEST.MF

. We will rename the manifest.txt to MANIFEST.MF and put it inside META-INF directory. Then run the below command to update the entry point.

要使用jar命令更新文件,文件结构应相似,否则它将新文件添加到另一个目录。 由于清单文件位于

META-INF/MANIFEST.MF

。 我们将manifest.txt重命名为MANIFEST.MF,并将其放入META-INF目录中。 然后运行以下命令更新入口点。

[email protected]:~/CODE/MyProject/bin$ jar uvfm MyProject.jar META-INF/MANIFEST.MF 
Jan 30, 2013 5:40:27 PM java.util.jar.Attributes read
WARNING: Duplicate name in Manifest: Main-Class.
Ensure that the manifest does not have duplicate entries, and
that blank lines separate individual sections in both your
manifest and in the META-INF/MANIFEST.MF entry in the jar file.
updated manifest
           

Note that warning is because of duplicate entries and manifest file can have only one entry for each meta property, but it still updates the entry point by updating the manifest file. Now when you will run the jar, it will execute the changed entry class.

请注意,警告是由于条目重复,清单文件的每个元属性只能有一个条目,但是它仍然通过更新清单文件来更新入口点。 现在,当您运行jar时,它将执行更改后的条目类。

What if we don’t want to change the entry point but want to execute another class from jar file. It seems confusing but solution is real simple. All you need to do it add the jar to class path and then execute the class like a normal java class with main method.

如果我们不想更改入口点,但想从jar文件执行另一个类,该怎么办。 似乎令人困惑,但解决方案确实很简单。 您所需要做的就是将jar添加到类路径,然后像使用main方法的普通java类一样执行该类。

[email protected]:~/CODE/MyProject/bin$ java -cp MyProject.jar com.journaldev.util.MyUtil
MyUtil main method
           

That’s all for how to run jar file in java with single entry point, different entry points and without any entry point at all using java classpath.

这就是如何使用Java类路径在具有单个入口点,不同入口点并且根本没有任何入口点的Java中运行jar文件的全部内容。

Reference: Oracle Doc

参考: Oracle文档

翻译自: https://www.journaldev.com/1344/run-jar-file

运行 jar 文件

继续阅读