The article directories

  • What is a JAR package
  • How do I create a JAR package
  • Run the jar package
  • Android Studio adds the downloaded JAR package
    • The first way: add as library
    • The second way

What is a JAR package

Since Java is a compiled language, the source files are.java, and the compiled.class files are the actual bytecode that can be executed by the JVM. If you have a lot of.class files, scattered in various levels of directories, it’s not easy to manage. It would be much more convenient if you could make the directory into a package and turn it into a file

Jar packages do just that. They print all the files (.class files and others) in the package hierarchy into a SINGLE JAR file, making it easier to back up and send to customers

The JAR package is actually a zip format compressed file, and the JAR package is the directory

How do I create a JAR package

To test this, let’s create a new Java project called TestApplication with IDEA



Then create a new Test class that will be passed in at runtime and output “Hello parameter.”

public class Test {
    public static void main(String[] args) {
        if ((args == null) || (args.length <= 0)) {
            System.out.println("Please pass the parameters");
            return;
        }
        System.out.println("Hello"+args[0]); }}Copy the code

First edit the parameters to be passed in at runtime. Right-click Test -> Create ‘test.man ()’…



The argument we enter here is Errol_King



Run the program and see “Hello Errol_King”



Can see out/production/TestApplication laying into the Test. The Java



Right-click the project -> select Open Moudule Settings

Select Artifacts -> JAR -> From Modules with dependencies



Select the Main Class



OK



The Build – > Build Artifacts…





Another JAR package was found in out/ Artifacts

Mainfest.mf was added to SRC



The manifest.mf content is as follows, and the main-class is Test

Run the jar package

Run CMD and enter the commandJava -jar Specifies the path of the jar packageIt’s ready to run. When entering the path of the JAR package, you can simply drag the JAR package into the CMD window

Android Studio adds the downloaded JAR package

The first way: add as library

1. Put the downloaded JAR package into the libs folder

2, select the jar package, right-click, add as library

3. Select Module and click OK

4. Observe app/build.gradle and add the jar package just now, indicating that the jar package is added successfully

The second way

Sometimes you can use this method without the Add as Library option when you right-click a JAR package into libs

Right-click the app and select Open Module Settings