The jar packages we have used over the years

Maybe love is just because of loneliness, you need to find someone to love, even if there is no ending


What are the jar packages we use daily:
 

        The jar package is Java Archive File. As the name suggests, its application is closely related to Java. It is a document format of Java and a platform-independent file format that can combine multiple files into one file.

        A jar package is very similar to a zip package - to be precise, it is a zip package, so it is called a file package. The only difference between jar and zip is that the content of the jar file contains a META-INF/MANIFEST.MF file. This file is automatically created when the jar file is generated. As the "details sheet" in the jar, it contains The version of the Jar package, the creator and the class search path Class-Path and other information. Of course, if it is an executable Jar package, it will contain the Main-Class attribute, indicating the Main method entry, especially the more important Class-Path and Main-Class. .

        In addition, it is worth noting that because the jar package mainly packages class files, and the class files generated by java compilation are platform-independent, this means that the jar package is cross-platform, so there is no need to worry about issues involving specific platforms. Speaking of the files in the jar, let’s take a look at the contents of the most common springboot project jar with static pages, and you will find that the decompressed jar is not simple (in addition to .MF and .class files, the jar also It can package everything needed for static resource files such as .html, .css and .js. This means that we can package our projects into jars, that is, whether they are web applications or underlying frameworks, they can be packaged into jars. Bag.)

        Running jar requires a java jdk environment, then right-click the jar package to open and select java(TM) to run, or it can be executed directly through the java -jar command.

        Some jars cannot be run directly because there is no main method entry.

        We all know that some classes can be executed because they have a main function, which is the entry point of the program. Similarly, there must be a .class file in the executable jar package that provides the main function. Make it executable. So here comes the question. There may be multiple .class files in a jar that all have main functions. How do I know which one to execute? In fact, the answer is very simple, just look at the Main-Class attribute in MANIFEST.MF mentioned earlier, which will specify the function entry.

Advantages of using jar packages:

        After developing a program, there are many classes in the program. If you need to provide it to others, it is very bad to send them a lot of source files. Therefore, it is usually necessary to package these classes and related resource files into a jar package. , provide this jar package to others for use.

        After others get the jar we provide, they can easily call it. And safe. Ability to digitally sign JAR files, allowing only users who can recognize the digital signature to use the contents.

        During the development process, we can also extract the common parts of our code, accumulate some common util classes, gradually modularize them, and finally make them into jar packages for use in other projects or modules, while constantly The advantage of polishing the contents of the jar and making it easier to understand and universal is that in addition to being a great help to your code refactoring ability and module abstraction ability, it is also a kind of long-term liberation. Your repetitive workload is a way to give you more energy to do other things.

How to create a jar package

To create a JAR file (Java Archive), you can use Java's command line tools jar. The following are the general steps for creating a JAR file:

  1. Write Java code : First, you need to write your Java code and make sure the code compiles successfully. public static void main(String[] args)Make sure you include a method in your code , as this is the entry point to the executable JAR file.

  2. Compile Java code : Use javacthe command to compile your Java code. For example: javac YourClass.java This will generate a YourClass.classbytecode file named.

  3. Create a manifest file : Create a Manifest.mfmanifest file named, which contains basic information of the JAR file, such as the main class (Main-Class). The contents of the manifest file might look like this: Manifest-Version: 1.0
    Main-Class: YourMainClass where YourMainClassshould be replaced with the fully qualified class name of your main class.

  4. Create a JAR file : Use jarthe command to create a JAR file. The following is an example command: jar cvmf Manifest.mf YourJarName.jar YourClass.class // cIndicates creating a JAR file.

    // vIndicates displaying detailed information on the console. // mIndicates that the manifest file is included. // fFollowed by the name of the JAR file. // YourClass.classIs the class file to be included in the JAR file. It can contain multiple class files, separated by spaces.
  5. Run the JAR file : Now, you can java -jarrun the created JAR file using the command. For example: java -jar YourJarName.jar These are the basic steps for creating and running a JAR file. Be sure to replace the class name and file name in the above example to suit your project. If you use other dependent libraries, you also need to include the class files of these libraries in the JAR file. This can jarbe done by including the corresponding classpath in the command.

How to run jar package

To run a JAR file, you use javathe command, followed by -jarthe flag, and then the name of the JAR file. Here are the general steps for running a JAR file:

  1. Open a command line terminal (command prompt or PowerShell on Windows, or a terminal window on Unix/Linux).

  2. Use the following command to run the JAR file:

    java -jar YourJarName.jar

    where YourJarName.jarshould be replaced with the actual name of the JAR file you want to run.

  3. Press the Enter key to execute the command.

If the JAR file contains a Main-Class, Java will execute the mainmethods of the Main-Class. If the JAR file is configured correctly, and your Java runtime environment is configured correctly, the Java application in the JAR file should be executed.

Please ensure that the Java Runtime Environment (JRE) is installed and the correct environment variables are set so that you can run javacommands from the command line. If your JAR file depends on other JAR files or libraries, make sure those dependencies are also on the classpath or include them in your JAR file.

Benefits of using jar packages

JAR package (Java Archive) is a common file format in Java programs and has the following important benefits:

  1. Modularity and organization : JAR packages allow multiple related classes and resources to be packaged into a single file, allowing for better organization and management of code. This helps modularize projects, making the code easier to maintain and distribute.

  2. Dependency management : JAR packages make it easier to manage project dependencies. You can package the external libraries and dependencies your project requires into JAR files and then reference them in your project. This simplifies building and deploying projects and reduces the possibility of dependency conflicts.

  3. Portability : JAR packages are cross-platform because they contain bytecode and resources and can run on any platform that supports Java. This means you can port JAR files from one environment to another without modifying the code.

  4. Version control : By using different versions of JAR packages, you can easily manage and switch between different versions of the project. This is useful for testing, rolling back, or upgrading applications.

  5. Security : JAR packages can be security verified using digital signatures to ensure they have not been tampered with. This helps ensure that your application is not at risk from malicious code injection.

  6. Resource Management : In addition to Java class files, JAR packages can also contain other resource files such as images, configuration files, and documentation. These resources can be easily packaged and distributed along with the code.

  7. Deployment Simplified : JAR packages make deploying Java applications very simple. Simply copy the JAR file to the target environment and run it without complicated installation procedures.

  8. Code reuse : JAR packages can contain common classes and libraries that can be shared and reused by multiple projects. This helps improve the maintainability and reusability of your code.

In summary, JAR packages are a key component in the Java ecosystem. They provide many benefits, including code organization, dependency management, cross-platform compatibility, and security, making it easier and more efficient to develop, deploy, and maintain Java applications.

Guess you like

Origin blog.csdn.net/s_sos0/article/details/127577329