天天看點

運作 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 檔案

繼續閱讀