search
HomeJavaJavaBaseHow to run jar package in java?

How to run jar package in java?

Dec 03, 2019 pm 03:24 PM
java

How to run jar package in java?

How to run jar package in java: (Recommended: java video tutorial)

Everyone knows that a java application project can be packaged into a jar, of course you must specify a main class with a main function as the program entry point of your jar package.

The specific method is to modify the MANIFEST.MF file under the META-INF directory in the jar package.

For example, there is a jar package called test.jar, which contains a main class with a main function: test.someClassName

We only need to add the following sentence to MANIFEST.MF:

Main-Class: test.someClassName

Then we can enter java -jar test.jar in the console to run the jar.

Methods to run third-party jar packages:

Method 1. Use Bootstrap Classloader to load these classes

We can use the following parameters at runtime :

-Xbootclasspath: Completely replaces the system Java classpath. It is best not to use it.
-Xbootclasspath/a: Loaded after the system class is loaded. Generally use this.
-Xbootclasspath/p: Load before loading the system class, pay attention to its use, it will not be good if it conflicts with the system class.

win32 java -Xbootclasspath/a: some.jar;some2.jar; -jar test.jar
unix    java -Xbootclasspath/a: some.jar:some2.jar: -jar test.jar

Each jar is separated by a semicolon in the win32 system, and a colon is used in the unix system. Separate

Method 2. Use Extension Classloader to load

You can throw all the jars that need to be loaded into %JRE_HOME%/lib/ext, in this directory The jar package will be loaded by Extension Classloader after Bootstrap Classloader completes its work. Very convenient and very worry-free. :)

Method three, still use AppClassloader to load, but no classpath parameter is required

We add the following code in MANIFEST.MF:

Class-Path: lib/some.jar

lib is a subdirectory in the same directory as test.jar, and the some.jar package to be referenced by test.jar is in it.

If there are multiple jar packages that need to be referenced:

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

Use spaces for each individual jar Just separate. Be careful to use relative paths.

Method 4. Customize Classloader to load

This method is the ultimate solution. Basically, all well-known java applications do this, such as tomcat and jboss etc.

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of How to run jar package in java?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor