Run jar application references are four ways other jar package

A java application project can be packaged into a jar, but you must specify a main class has the main function of your program as this jar package entrance.

Specific method is modified in the MANIFEST.MF file jar package directory META-INF.

 

 

For example, a man named wqbin.jar jar package, which has a has a main function of the main class: wqbin.MainClass1

We just add the following sentence at the MANIFEST.MF inside:

Main-Class: wqbin.MainClass1

 Then we can enter in the console

java -jar  wqbin.jar

That can run the jar.

But this project we need to refer to other third-party jar package, which was referenced in the eclipse is called some.jar package in the form of project jar package, when placed under the lib subdirectory of the project,

The last item packed this some.jar also call came, but by the time the implementation of this wqbin.jar java -jar message can not find Class abnormal, because that is not on its own internal references jar jar package.

How to do that?

Runtime classpath way to add it okay? It is added while running jar classpath parameters:

java -classpath some.jar  -jar wqbin.jar

This approach is not ideal because the jar is specified using the classpath to load the AppClassloader, java -jar parameter added to the command later, AppClassloader only focus on class within the scope of the test.jar, classpath parameter failure.

Then how to reference other jar package it?

 

A method using Bootstrap Classloader to load these classes.

 

We can at runtime using the following parameters:

- Xbootclasspath: completely replace the system Java classpath not the best.
-Xbootclasspath / A: class loading to load in the system. The general use.
-Xbootclasspath / p: before the system class loader to load, pay attention to use, and the system class conflict on the bad.
 
 
win32     java -Xbootclasspath/a: some.jar;some2.jar;  -jar wqbin.jar
unix          java -Xbootclasspath/a: some.jar:some2.jar:  -jar wqbin.jar
Each jar win32 system separated by semicolons, colons the unix system

 

The second method for loading using Extension Classloader

 

You may need to load the jar to have thrown% JRE_HOME% / lib / ext below, jar package in this directory will be loaded by Extension Classloader in Bootstrap Classloader after work.

 

Method three, or use AppClassloader to load, but does not require the classpath parameter

 

We add the following code in the MANIFEST.MF:

Class-Path: lib/some.jar

 

lib is a directory with subdirectories and wqbin.jar, test.jar to reference some.jar package in there.

Then test run, everything is normal!

 

If there is a plurality jar package need to reference:

Class-Path: lib/some.jar lib/some2.jar

Each individual jar separated by a space on it. Use a relative path.

Another: If the META-INF contains INDEX.LIST files, it may cause the Class-Path configuration failure. INDEX.LIST is generated when Jar packaging tools package index files, delete does not affect the operation.

 

Method four, to load custom Classloader

This method is the ultimate solution, basically those famous java applications are so dry, such as tomcat, jboss, and so on.

 

to sum up:

Above four methods can be used, in particular, the program runs in a very simple environment when. However, if the runtime environment in a multi-tasking, multi-application, each application can best independent of each other, the first and second programs are likely to have an impact on other applications, so the best is to choose a third and the fourth seed.

Guess you like

Origin www.cnblogs.com/wqbin/p/11122709.html