1. Why packaging
There are different opinions on this issue on the Internet. Based on personal understanding and online opinions, it is packaged as a jar package for the convenience of others. If you are running a java program, you do not need to look for the class containing the main method to execute; if you are using a third-party jar package, import the jar package directly into your own project instead of copying a bunch of class files. Building a war package is the web application deployment method chosen in real production environments. It is said online that this method will not cause file loss like directly copying folders, and the server will optimize the application, such as deleting empty folders, etc. The above is for information only.
2. How to package
The local environment is windows 10, jdk 1.8
The same tool jdk/ is used to create jar or war packages bin/jar.exe
1. Pack it into a jar package
|-----------------You can skip it, just for the convenience of understanding the packaging Things to note------------------|
Project Introduction
Database table structure and table creation statements
CREATE TABLE `customer` ( `cust_id` int(11) NOT NULL AUTO_INCREMENT, `cust_name` char(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `cust_address` char(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `cust_city` char(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `cust_state` char(5) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `cust_zip` char(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `cust_country` char(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `cust_contact` char(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `cust_email` char(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`cust_id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 10006 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; INSERT INTO `customer` VALUES (10001, 'Coyote Inc.', '200 Maple Lane', 'Detroit', 'MI', '44444', 'china', 'Y Lee', 'ylee@coyote.com'); INSERT INTO `customer` VALUES (10002, 'Mouse House', '333 Fromage Lane', 'Columbus', 'OH', '43333', '', 'Jerry Mouse', NULL); INSERT INTO `customer` VALUES (10003, 'Wascals', '1 Sunny Place', 'Muncie', 'IN', '42222', 'USA', 'Jim Jones', 'rabbit@wascally.com'); INSERT INTO `customer` VALUES (10004, 'Yosemite Place', '829 Riverside Drive', 'Phoenix', 'AZ', '88888', 'UK', 'Y Sam', 'sam@yosemite.com'); INSERT INTO `customer` VALUES (10005, 'gzn or 1=1', '4545 53rd Street', 'Chicago', 'IL', '54545', '', 'E Fudd', NULL);
Project structure
app.java
package com.gzn.demo; import java.sql.*; import java.util.Scanner; /** * @author: gzn * @date: 2019/4/13 10:53 */ public class App { public static void main(String[] args) { int count = Integer.valueOf(args[0]); System.out.println("请输入要查询用户的条数?(0到5之间):"); Scanner sc = new Scanner(System.in); int count = sc.nextInt(); String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/corejava"; String username = "root"; String password = "root"; String sql = "select cust_id, cust_name, cust_address, cust_city from customer limit 0, ? "; try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, username, password); PreparedStatement pstat = conn.prepareStatement(sql); pstat.setInt(1, count); ResultSet rs = pstat.executeQuery(); while(rs.next()) { System.out.println("cust_id:" + rs.getObject("cust_id").toString()); System.out.println("cust_name: " + rs.getObject("cust_name").toString()); System.out.println("cust_address: " + rs.getObject("cust_address").toString()); System.out.println("cust_city:" + rs.getObject("cust_city").toString()); System.out.println("----------------------" +"\n"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } }
|----------- -------------------------------------------------- ----------------|
1.1. Manual packaging using jar
(1) Use cmd to find the path of the project compilation output
(2) Run the command jar -cvf helloworld.jar .
-c (create , create a file when creating a) table
-v (verbose, lengthy, detailed) Print compression details on the console
-f (filename) Specify the compressed file name
helloworld.jar The file name can be customized Definition
. Indicates all files in the helloworld directory. Be sure to write "." here, otherwise errors may occur. (Supplementary, * asterisks are also acceptable)
So far, the packaging is successful, but it cannot be run. If you want it to work, you need to modify the MANIFEST.MF file in helloword.jar.
(3) Use the decompression tool to open helloword.jar and edit META-INF/MANIFEST.MF to add attributes
MANIFEST.MF initial state
Manifest-Version: 1.0 Created-By: 1.8.0_161 (Oracle Corporation)
Add attributes: ( Note that the colon is an English colon and there is a space after the colon )
Main-Class: The class containing the main method
Class-Path: The path of the dependent jar package. If you rely on multiple jar packages, use spaces to separate them.
Path: relative path, the path of the jar package relative to the helloworld.jar file
Absolute path, the jar package is in the operating system Path
Commonly used relative paths, put the dependent jar package and your own jar package in the same directory, so that Class-Path can directly write the name of the dependent jar package.
Status after adding attributes:
Manifest-Version: 1.0 Created-By: 1.8.0_161 (Oracle Corporation) Class-Path: mysql-connector-java-5.1.18.jar Main-Class: com.gzn.demo.App
(4) Run the test
Copy the dependencies to the directory of the same level as helloworld.jar, and use java -jar helloworld.jar to run the program.
If the jar package is only for use by other developers and does not need to be run, proceed to step (2) .
1. 2. Use IDEA for packaging
##Main Class: Contains the main method Class;
extract to the target JAR: Extract the target jar. This option requires you to configure the absolute path for the dependent jar.
copy to the output directory and link via manifest: Copy the dependent jar to the output directory, that is, at the same level as the jar packaged in your project. In this way, IDEA can directly configure the relative path for the Class-Path attribute in MENIFEST.MF.
首先进入jar包输入路径C:\Users\gzn\Desktop\helloworld\out\artifacts\HelloWorld_jar;
运行 java -jar helloworld.jar;
2、打成war包
comment是我的一个已将编译好的web项目,使用cmd进入comment目录下执行命令
jar -cvf comment.war .
注意在项目目录下执行命令, “.” 表示对项目目录下的所有文件进行打包,将打包好的项目复制到Tomcat/webapps目录下,启动Tomcat服务器,就可以进行测试了。
The above is the detailed content of How to package a Java or Java Web project into a JAR package or WAR package. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools