jar、war的打包部署



1. 打包jar

自己写了一个类,怎么才能打包使用呢?下面就来介绍如何将自己写的类打包成jar包方便以后使用


1.1 准备一个写好的类

这里笔者写了一个基数排序的类

package com.howl.sort;

public class RadixSort {
	
	// 返回最大值
	private static int findMax(int[] arr){
		int temp = arr[0];
		for(int value : arr){
			if(temp < value){
				temp = value;
			}
		}
		return temp;
	}
		
	public static void radixSort(int[] arr){
		
		int max = findMax(arr);
		
		// 比较次数由最大值的位数决定
		for(int i = 1; max / i > 0; i *= 10){
			// 每一次新建默认是为0
			int[][] buckets = new int[arr.length][10];
			// 将每一个值根据当前比较的位数放入桶中
			for(int j = 0; j < arr.length; j++){
				int num = (arr[j] / i) % 10;
				buckets[j][num] = arr[j];
			}
			int k = 0;
			// 从上往下,从左往右收集
			// 从左往右移动是位数不同了
			// 从上往下是当前位数相同,但之前位数大小不同,上小下大
			for(int m = 0; m < 10; m++){
				for(int n = 0; n < arr.length; n++){
					if(buckets[n][m] != 0){
						arr[k++] = buckets[n][m];
					}
				}
			}
		}
	}
}

1.2 把类打包成jar

  • 笔者这里使用MyEclipse 2017,对应项目右键,选择Export


  • 接着选择JAR file


  • 选择要导出的包和类,以及导出地址,直接finish


  • 至此选择的路径下就会有一个压缩的jar包


1.3 使用jar包

  • 将打包好的jar包添加进Build Path


  • 新建测试类

这里要注意,使用前要先导包

import com.howl.sort.RadixSort;

public class JarTest {
	
	public static void main(String[] args) {
		
		int[] arr = {100,20,1,50,80};
		
		RadixSort.radixSort(arr);
		
		for(int value : arr){
			System.out.println(value);
		}
	}
}
1
20
50
80
100




2. 打包并部署war

完成了一个javaEE项目后,怎么把项目部署到Tomcat上去呢?可以打包成war包,然后放到tomcat的webapp目录下


2.1 准备一个javaEE项目

这里举例笔者之前学javaweb的小项目,就是登录与写问题的操作而已,没什么可说的,就把目录结构放出来吧


2.2 打包成war,和打包jar大同小异

  • 打包


2.3 部署

其实就是把打包成的war包放入Tomcat的webapp目录下,然后启动汤姆猫就可以访问了


  • 把war放入webapp目录下


  • 启动Tomcat

这里没什么好说的


  • 访问



原文地址:https://www.cnblogs.com/Howlet/p/12248555.html