2023年12月2日发(作者:)

运行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 fromcommand 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 used by java command to execute the class. My project structure looks likebelow -Class是jar的入口点,由java命令用于执行该类。 我的项目结构如下图所示。I have to add manifest file in jar file, so I will create a manifest file with below details.我必须在jar文件中添加清单文件,因此我将使用以下详细信息创建清单文件。n-Class: Now we will use jar command to create jar file with our manifest file data.现在,我们将使用jar命令使用清单文件数据创建jar文件。pankaj@JD:~/CODE/MyProject/bin$ jar cvfm comadded manifestadding: 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/(in = 444) (out= 303)(deflated 31%)adding: com/journaldev/util/(in = 0) (out= 0)(stored 0%)adding: com/journaldev/util/(in = 444) (out= 304)(deflated 31%)Now when I unpack and check the contents of file, it contains following data.现在,当我打开包装并检查文件的内容时,它包含以下数据。Manifest-Version: 1.0Created-By: 1.6.0_37 (Apple Inc.)Main-Class: Manifest-Version and Created-By entries are added by jar command. Now we are ready to run jar file through command st-Version和Created-By条目由jar命令添加。 现在,我们准备通过命令行运行jar文件。从命令行运行Jar文件 (Run Jar File from command line)pankaj@JD:~/CODE/MyProject/bin$ java -jar

MyUtil main methodSo 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 canuse 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 manifest file is located at META-INF/. We will rename the to and put it insideMETA-INF directory. Then run the below command to update the entry point.要使用jar命令更新文件,文件结构应相似,否则它将新文件添加到另一个目录。 由于清单文件位于META-INF/ 。 我们将重命名为,并将其放入META-INF目录中。 然后运行以下命令更新入口点。pankaj@JD:~/CODE/MyProject/bin$ jar uvfm META-INF/

Jan 30, 2013 5:40:27 PM utes readWARNING: Duplicate name in Manifest: that the manifest does not have duplicate entries, andthat blank lines separate individual sections in both yourmanifest and in the META-INF/ entry in the jar d manifestNote that warning is because of duplicate entries and manifest file can have only one entry for each meta property, but itstill updates the entry point by updating the manifest file. Now when you will run the jar, it will execute the changed entryclass.请注意,警告是由于条目重复,清单文件的每个元属性只能有一个条目,但是它仍然通过更新清单文件来更新入口点。 现在,当您运行jar时,它将执行更改后的条目类。What if we don’t want to change the entry point but want to execute another class from jar file. It seems confusing butsolution 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 withmain method.如果我们不想更改入口点,但想从jar文件执行另一个类,该怎么办。 似乎令人困惑,但解决方案确实很简单。 您所需要做的就是将jar添加到类路径,然后像使用main方法的普通java类一样执行该类。pankaj@JD:~/CODE/MyProject/bin$ java -cp MyUtil main methodThat’s all for how to run jar file in java with single entry point, different entry points and without any entry point at all usingjava classpath.这就是如何使用Java类路径在具有单个入口点,不同入口点并且根本没有任何入口点的Java中运行jar文件的全部内容。Reference:参考:运行 jar 文件