Several ways of playing jar package in Java
1、 Make a jar package that contains only bytecode files
Let's first look at how to make a jar package containing only bytecode files, that is, only class files. This is the simplest form
1. The simplest jar package -- directly output Hello
Finally generated jar package structure
Method steps
(1) Write a hello. Java file with Notepad
class Hello{ public static void main(String[] agrs){ System.out.println("hello"); } }
(2) Enter the directory with the command line and compile the file
javac Hello. java
(3) Type the compiled hello. Class file into a jar package
jar -cvf hello. jar Hello. class
C means to create a new jar package, V means to output some information about the creation process on the console during the creation process, and f means to name the generated jar package
(4) Run the jar package
java -jar hello. Jar will report the following error hello There is no main manifest attribute in the jar
Add main class attribute
Open hello.com with compression software Jar, you will find that there is an additional meta-inf folder, which contains a menifest MF file, open with Notepad
Write main class: hello at the position of the third line (note that there is a space after the colon, and there is an empty line at the end of the whole file), and save
Run Java - jar hello. Exe again Jar. At this time, you can see hello on the console. Success
2. Jar package with two classes -- output Hello by calling
Finally generated jar package structure
Method steps
(1) Write a hello. Java and a Tom. Java file with Notepad
The goal is to have Hello call Tom's speak method
class Hello{ public static void main(String[] agrs){ Tom.speak(); } } class Tom{ public static void speak(){ System.out.println("hello"); } }
(2) Compile: javac hello.java
Hello Java and Tom Java is compiled at the same time, because Tom is invoked in Hello, and Tom is also found in the process of compiling Hello.
(3) In the jar package, this time we define the main class directly in another way.
Prepare the above menifest in advance MF file and store it in the meta inf folder. At this time, the command to type jar package is as follows
jar -cvfm hello. jar Meta-INF\MENIFEST. MF Hello. class Tom. class
This command means to use the first file as menifest MF file, hello Jar as the name, put hello Class and Tom Class into jar package. One more parameter m indicates that the menifest file is to be defined
(4) Run Java - jar hello.jar, and you will see hello on the console. Success
3. Jar package with directory structure -- output Hello by quoting the package and calling
Finally generated jar package structure
We changed the previous one slightly, put the Tom class under the com package, and the directory structure of the source file becomes
And Tom Java needs to declare its own package name on the first line
package com;
Hello. Java needs to introduce the Tom class, and also import in the first line
import com. Tom;
Method steps
(1) Compile hello.java
(2) Open the jar package and also prepare the menifest file
jar -cvfm hello. jar Meta-INF\MENIFEST. MF Hello. class com
Note that the last com means to type all the files in the com folder into the jar package
(3) Run Java - jar hello.jar, and you will see hello on the console. Success
(4) Optimization process
We noticed that there is Tom under the com package Java source files are also typed into the jar package, which is not good. Can you optimize the javac command to compile all compiled files to another isolated place? The answer is yes.
Compiling hello Java, create a new target folder first. Then we use the following command
javac Hello. java -d target
This command means that all compiled files are placed in the target folder.
Copy the meta inf folder to the target directory, enter the directory, and enter the following command
jar -cvfm hello. jar Meta-INF\MENIFEST. MF *
Note that the last position becomes *, which means that all files in the current directory are typed in the jar package
Optimization completed
So far, we can conclude that to create a jar package containing only class bytecode files, the following command is sufficient
Javac file to compile -d target location
Jar - cvfm name menifest file file file to package 1 file to package 2
2、 Make a jar package containing jar files
Let's make the scenario a little more complicated and take a look at the scenario where other jar packages need to be introduced into the jar package
1. Two jar packages call each other -- call the jar outside the jar to output Hello
Finally generated jar package structure
Method steps
Preparation: put the Tom without package written in the above one Copy the jar (to call the speak method inside)
(1) Write a hello.java and compile it into hello.class. Note that since the speak method of Tom class is referenced in Hello, you should use the - CP parameter to import the tom.jar package into the jar package
javac -cp tom. jar Hello. class
Here - CP stands for - classpath, which refers to Tom Add jar to the classpath path
(2) Complete the hello.class into a jar package, and the steps are omitted
(3) At this time, run Java - jar and find the error classnotfoundexception: Tom
The reason is very simple. The introduction of jar package needs to be in menifest Configure a new attribute in MF file: class path, which points to all jar packages you need
Now menifest MF this file should become
(4) OK, modify this file and run it again. It is found that hello is successfully output on the console
Tips: introduce multiple jar packages separated by spaces
So far, we can conclude that the command changes are as follows
javac -cp xxx. Jar file to compile - D target location
Jar - cvfm name menifest file file file to package 1 file to package 2
2. Jar package contains jar package -- call jar in jar to output Hello
Finally generated jar package structure
When we type the required third-party jar package into our own jar package in the project, if we still follow the above operation method, we will report that the class exception is not found. The reason is that jar cannot refer to the jar package placed inside itself.
The specific implementation details of this situation are complex. I will introduce how some well-known Java applications load jar packages in the next article to illustrate this situation. For a brief description of the implementation method, please refer to this article:
https://www.jb51.net/article/174315.htm
3、 Make a jar package containing resource files
1. The resource file is inside the jar package -- read the file in the jar
Finally generated jar package structure
Method steps
import java.io.InputStream; import java.io.BufferedReader; import java.io.InputStreamReader; class Hello{ public static void main(String[] args) throws Exception{ Hello hello = new Hello(); InputStream is = hello.getClass().getResourceAsStream("text.txt"); print(is); } /** * 读取文件,输出里面的内容,通用方法 */ public static void print(InputStream inputStream) throws Exception { InputStreamReader reader = new InputStreamReader(inputStream,"utf-8"); BufferedReader br = new BufferedReader(reader); String s = ""; while ((s = br.readLine()) != null) System.out.println(s); inputStream.close(); } }
2. The resource file is inside another jar package -- read the file in another jar
Finally generated jar package structure
Method steps
The same as 1, except that you need to add resource. In the menifest file Add jar to classpath
import java.io.InputStream; import java.io.BufferedReader; import java.io.InputStreamReader; class Hello{ public static void main(String[] args) throws Exception{ Hello hello = new Hello(); InputStream is = hello.getClass().getResourceAsStream("text.txt"); print(is); } /** * 读取文件,输出里面的内容,通用方法 */ public static void print(InputStream inputStream) throws Exception { InputStreamReader reader = new InputStreamReader(inputStream,"utf-8"); BufferedReader br = new BufferedReader(reader); String s = ""; while ((s = br.readLine()) != null) System.out.println(s); inputStream.close(); } }
3. The resource file is outside the jar package -- read the file outside the jar
Finally generated jar package structure
Method steps
import java.io.InputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.FileInputStream; class Hello{ public static void main(String[] args) throws Exception{ Hello hello = new Hello(); InputStream is = new FileInputStream("text.txt"); print(is); } /** * 读取文件,输出里面的内容,通用方法 */ public static void print(InputStream inputStream) throws Exception { InputStreamReader reader = new InputStreamReader(inputStream,"utf-8"); BufferedReader br = new BufferedReader(reader); String s = ""; while ((s = br.readLine()) != null) System.out.println(s); inputStream.close(); } }
summary
The above is a detailed explanation of several methods of Java jar package introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!