This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.
I found myself and my classmates had trouble remembering many of the utility commands, especially tar and JAR commands, which were programmed for Google.
Archive command -tar
Tar is an old Linux command that can handle 80% of your backup needs if you know the basics, and the finer details.
The basic function
The basic function is archive and extract.
Archiving and extraction
The basic function of tar is to archive multiple directories into an archive and extract the original contents from the archive. As follows:
- When we archive multiple file fire directories into one archive file, we need to use the -c function,
tar -cf archive.tar foo bar
Archive foo bar in archive.tar. - When we extract the original file contents from the archive, we need to use the -x function,
tar -xf archive.tar
Extract files from archive.tar to the current directory.
We can find that f option is used after basic functions. If f option is removed, it will not work properly. Why?
Because in tar design -f is followed by the name of the archive file, such as archive.tar; Whether archived or extracted, the f is followed by the name of the file to be archived.
We need to know the contents of the archive to extract the files to the directory (otherwise there may be security issues), so tar also provides the ability to query the contents of the archive.
TAble to list
T (table) lists the contents of the package. -v displays the detailed execution process of the function action, as follows:
# tar tvf archive.tar
-rwxr-xr-x root/root 106 2021-11-15 21:48 hello
-rw-r--r-- root/root 0 2021-11-15 23:18 hello2
Copy the code
Tar tar TVF archive.tar is equivalent to tar -tvf archive.tar.
More features – detail & efficiency
Tar also provides many details:
- Additional content
- Extract part of content
- Create a process that can exclude files
- Compression and decompression
- -j Supports the BG2 compression format
- -z Supports GZ compression
With the addition of additional features, the entire tar function diagram comes to life:
- -r append
tar -rvf archive.tar /zhangsan
You can add /zhangsan directory to archive.tar - You can also extract only one file and specify the directory to extract to, here
tar -xf archive.tar file1 -C /tmp/
Extract file1 to the specified directory/TMP / - -p is not followed by any parameter. The file permission is reserved for archiving
- Both -z and -j indicate that the archive file is compressed, but in different formats
- For archive files in tar.gz format, add -z and tar.bzip to archive and compress them
tar -cvzf backup.tar.gz ...
Create a tar.gz archive. For such files, t and x operations can be performed without the z option.
Java packaging-JAR package
Jar is very similar to tar. Jar (Java Archive) can package and compress multiple source and resource files into an Archive. Update, view and extract are also supported. More importantly, JAR packages can be packaged according to the conventions that various Java programs run on.
The basic function
- The basic function is also c x T, these options are basically the same as the corresponding use of tar
- U Updates the file, equivalent to r in tar
- V: Displays the detailed process
- F: Specify the name of the JAR file
The details are shown in the following figure
As you can see, jar and tar are very similar in function, even when extracting and viewing, using tar directly is also possible.
/ manifest.mf/manifest.mf/manifest.mf/manifest.mf/manifest.mf
# jar cvf doc.jar A.md
# jar tf doc.jar
META-INF/
META-INF/MANIFEST.MF
A.md
#jar xf doc.jar META-INF/MANIFEST.MF
# cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: 12.0.2 (Oracle Corporation)
Copy the code
It can also be viewed using the unzip -p console:
#unzip -p doc.jar META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: 12.0.2 (Oracle Corporation)
Copy the code
Jar package runs: manifest file
What is meta-INF/manifest.mf? Why meta-INF/manifest.mf?
Even if you do nothing, look at the contents of the manifest.mf file to see that it records the version information of the JAR package and when it was created. The most important thing is that Java treats jar packages as runnable programs, specifying entry points to run programs directly in Java-JAR fashion.
As shown below:
- -e option: To specify the Class of the main method, the listing file will have an extra line main-class: com.ywz.main
- -m option: create your own manifest file and write it to main-class: com.ywz.main;
Demo: Using a simple Java class:
//$ cat com/ywz/Main.java
package com.ywz;
public class Main{
public static void main(String[] args) {
System.out.println("Hello,My Reader!"); }}Copy the code
Operation demonstration:
$ javac com/ywz/Main.java# compiler
$ jar cvf example.jar com/ywz/Main.classAdded list being added: com/ywz/ main. class(input = 426) (output = 298)(compressed by 30%)$ jar tf example.jar #list the contents of the jar package
META-INF/
META-INF/MANIFEST.MF
com/ywz/Main.class
$ unzip -p example.jar META-INF/MANIFEST.MF # Basic information
Manifest-Version: 1.0
Created-By: 12.0.2 (Oracle Corporation)
##cp specifies that the class-path is example.jar. By default, Java programs will search for classes from example.jar
$ java -cp example.jar com.ywz.Main Com.ywz.main; com.ywz.main
Hello,My Reader!
#If cp is not specified, this is the current path
$ java com/ywz/Main Com.ywz.main and com.ywz.main can be used to run the current path
Hello,My Reader!
## -e specifies main-class: com.ywz.main
$jar cvfe example.jar com.ywz.Main com/ywz/Main.classAdded list being added: com/ywz/ main. class(input = 426) (output = 298)(compressed by 30%)$ unzip -p example.jar META-INF/MANIFEST.MF # verify that main-class is added
Manifest-Version: 1.0
Created-By: 12.0.2 (Oracle Corporation)
Main-Class: com.ywz.Main
$ java -jar example.jar # - run the jar
Hello,My Reader!
## -m specifies main-class: com.ywz.main
$ echo "Main-Class: com.ywz.Main" > example_manifest.txt
$jar cvfm example.jar example_manifest.txt com/ywz/Main.classAdded list being added: com/ywz/ main. class(input = 426) (output = 298)(compressed by 30%)$ unzip -p example.jar META-INF/MANIFEST.MF # verify that main-class is added
Manifest-Version: 1.0
Main-Class: com.ywz.Main
Created-By: 12.0.2 (Oracle Corporation)
$ java -jar example.jar # - run the jar
Hello,My Reader!
Copy the code
-
Javac compiles Java files
-
Jar is the default Class loader to search for the Class, so it needs to know which Class is the main Class.
-
-e or -m specifies main-class: com.ywz.Main
Maven packaging
While the low-level functionality of JARS described above, maven, a more advanced tool, is now commonly used to help build and package Java programs. Program directory structure:
$ tree. ├ ─ ─ pom. XML ├ ─ ─ the SRC │ └ ─ ─ the main │ └ ─ ─ Java │ └ ─ ─ the main, Java$ cat pom.xml
Copy the code
Because the code is simple, I did not upload github source code
- Main.java is the same as described above
- Pom file configuration:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ywz</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<properties>
<argLine>-Dfile.encoding=UTF-8</argLine>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.ywz.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Copy the code
- Using the maven-jar-plugin, specify that the mainClass for archive manifest is com.ywz.main
- The generated JAR package is similar to the jar command generated earlier, and runs the same. As follows:
$The tar tf target/test - 1.0. The jar# META-INF/ Maven
META-INF/
META-INF/MANIFEST.MF
com/
com/ywz/
com/ywz/Main.class
META-INF/maven/
META-INF/maven/com.ywz/
META-INF/maven/com.ywz/test/
META-INF/maven/com.ywz/test/pom.xml
META-INF/maven/com.ywz/test/pom.properties
$Unzip the -p target/test - 1.0. Jar meta-inf/MANIFEST. MFThe # tag is created by Apache Maven 3.6.1
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.6.1
Main-Class: com.ywz.Main
$Java - jar target/test - 1.0. The jar# run
Hello,My Reader!
Copy the code
As can be seen:
- The jar contains only meta-INF/Maven information
- The meta-INF/manifest.mf tag was created by Apache Maven 3.6.1.
Once you understand jar usage, you won’t be blinded by Maven’s various plug-in features, and it’s easy to find JAR packaging and plug-in logic!