When we package a runnable jar package in eclipse, there are three options in Library handing:
So what is the difference between these three packaging methods of Runnable JAR file in eclipse?
The first Extract required libraries into generated JAR
means decompress the dependent jars and turn them into classes and throw them into the jars you generate; the
second Package required libraries into generated JAR
means don’t decompress the dependent jars and put them directly into the jars you generate; the
third Copy required libraries into a sub-folder next to the generated JAR
means to relied on Put the jar in the "xxx_lib" folder in the same level directory where you generated the jar.
Let's use an example to illustrate. Now I have a project here TestExport
. The structure of this Java project is as follows.
Now in the Test class, an external jar package written by myself is referenced Command.jar
. Let's now use the above three methods to export to see how the effect is
Use the first method to export ( Extract required libraries into generated JAR
)
Method of exporting Runable Jar file
Select the right project to be exported, the election Export
.
Select Java-->Runable JAR file-->Next
the Launch configuration
entry in the selection process, which is the class where the main method.
In the Export destination
Select export position, click Browse...
to select the location you want to export.
Then Library handing
select Extract required libraries into generated JAR
the decompression dependent package mode in and
click to Finish
export
After exporting, find the jar package and open
it with a compression tool. You can see the contents inside.
You can see that the first directory is the package directory of our project, and the contents are as follows.
The second directory is the configuration file directory, this directory is bound to be there, don’t care about it.
The third directory is obtained by decompressing the dependency package Command.jar, and the contents are as follows.
Use the second method Runable JAR file
The export method is similar to the previous one, so I won't talk about it again.
This time choose to export in the second way and save it to another directory, in order to avoid confusion.
Then, find the jar and open it with a compression tool.
The result of opening is as follows.
You can see this way, right? The external dependency package Comman.jar is copied to the executable jar package Test.jar package we exported. Instead of extracting the Command.jar package to Test.jar as above.
Use the third method to export Runable JAR file
Still export to another directory in the same way,
find the exported jar, and open the compression tool.
You can see that there are now more folders in the same level directory of Test.jar Test_lib
, Test_lib
and the external dependency package Command.jar
and Test are stored in it . The jar is clean, only the files in the original project and the configuration file. The
configuration file specifies the path of the dependency package Command.jar in the same directory.
If we cut Test.jar to another place, then Test.jar , The dependency package in Test_lib will not be found, so it will run wrong.
For example, cut Test.jar to the upper-level directory and enter the java -jar Test.jar
running program in cmd . At this time, the program will throw an exception:
Exception in thread "main" java.lang.NoClassDefFoundError: system/call/cmd/Command
- 1
The difference between these three is like this