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

更新时间:2023-05-12 23:21:16 阅读: 评论:0

运⾏jar⽂件_如何运⾏JAR⽂件
运⾏ jar ⽂件
燕麦片怎么吃Today we will learn how to run JAR File in java. In last tutorial we learned how to . 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⽂件 (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 ud 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⽂件中添加清单⽂件,因此我将使⽤以下详细信息创建清单⽂件。
<
<
Main-Class: com.journaldev.util.MyUtil
Now we will u jar command to create jar file with our manifest file data.
现在,我们将使⽤jar命令使⽤清单⽂件数据创建jar⽂件。
pankaj@JD:~/CODE/MyProject/bin$ jar cvfm MyProject. 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)
pankaj@JD:~/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 u the jar command to update the manifest file.
如果我的项⽬有多个⼊⼝点,并且我想更改⼊⼝点⽽不再次创建jar,该怎么办。 因此,我们可以使⽤jar命令来更新清单⽂件。
For updating a file using jar command, file structure should be similar otherwi it will add new file to another directory. Since manifest file is located at META-INF/MANIFEST.MF. We will rename 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.MF,并将其放⼊META-INF⽬录中。 然后运⾏以下命令更新⼊⼝点。
pankaj@JD:~/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 parate individual ctions in both your
manifest and in the META-INF/MANIFEST.MF entry in the jar file.
updated manifest
Note that warning is becau 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 ems 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类⼀样执⾏该类。
pankaj@JD:~/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:
参考:
运⾏ jar ⽂件

本文发布于:2023-05-12 23:21:16,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/605777.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:命令   结构   包含   创建
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图