Step 1: Create a directory and copy all dependent JAR packages to this directory. For example, my test project depends on two JAR packages: classes12.jar and log4j-1.2.14.jar
The second step: write the program, it is no problem to write first, I only wrote one class, with the main function, the package name is test: test.Main
Main.java
- package test;
- import org.apache.log4j.Logger;
- public class Main {
- private Logger log = Logger.getLogger("R");
- public Main(){
- log.debug("********************");
- }
- public static void main(String[] args) {
- Main m = new Main();
- }
- }
The above sample program does not use classes12.jar, but depends on classes12.jar just to demonstrate the dependency addition of multiple jar packages
third step:
Start typing the jar package as shown:
The MANIFEST.MF file generated after that has only two lines:
Manifest-Version: 1.0
Main-Class: test.Main
In this case, you need to manually modify this file to:
Manifest-Version: 1.0
Class-Path: classes12.jar log4j-1.2.14.jar
Main-Class: test.Main
Note that a line has been added. In the Class-Path line, the name of the dependent JAR package is written, separated by spaces. If there is a subdirectory, write the relative directory, which is the relative path relative to the batch file.
Then repackage and export the JAR, as shown in the figure:
There is no need to modify MANIFEST.MF when you type this jar package again in the future, but if you add a dependent jar package, you only need to manually write the dependent jar package name in this file and save it. The executed jar package no name
Step 4: Write the batch
File name: run.bat
content:
java -jar TestJar.jar
Final directory structure:
Step 5: Test
Run cmd and execute in sequence:
d:
cd temp / TestJar
run
Needless to say
URL:http://stride.iteye.com/blog/1350156