Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create and execute .jar file under Linux

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to create and execute .jar files under Linux. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Jar file is a software package file format, which is usually used to aggregate a large number of Java class files, related metadata and resources (text, pictures, etc.) files into one file in order to develop Java platform applications or libraries.

How to create a JAR file in Linux

1. First, write a simple Java class with the main method for demonstration purposes for an application called HowtoingApp.

$vim HowtoingApp.java

Copy and paste the following code into the HowtoingApp.java file.

Public class HowtoingApp {public static void main (String [] args) {System.out.println ("Just executed HowtoingApp!");}}

Save the file and close it.

two。 Next, we need to compile and package the class into a JAR file using the javac and jar utilities, as shown in the figure.

$javac-d. HowtoingApp.java$ ls$ jar cvf howtoingapp.jar HowtoingApp.class$ ls

3. After creating the howtoingapp.jar, you can now execute the file using the java command, as shown in the figure.

$java-jar howtoingapp.jarno main manifest attribute, in howtoingapp.jar

From the output of the above command, we encountered an error. JVM (Java virtual machine) could not find our main manifest property, so the main class containing the main method (public static void main (String [] args)) could not be found.

The JAR file should have a listing containing a line * *: classname** in the form of Main-Class that defines a class with a main method that serves as the starting point for the application.

4. To resolve the above errors, we need to update the JAR file to include the manifest properties and code. Let's create a MANIFEST.MF file.

$vim MANIFEST.MF

Copy and paste the following line into the MANIFEST.MF file.

Main-Class: HowtoingApp

Save the file, and then use the following command to add the file MANIFEST.MF to howtoingapp.jar.

$jar cvmf MANIFEST.MF howtoingapp.jar HowtoingApp.class

5. Finally, when we execute the JAR file again, it should produce the expected results, as shown in the output.

$java-jar howtoingapp.jarJust executed howtoingApps $man java$ man javac$ man jar above is all the content of the article "how to create and execute .jar files under Linux". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report