1.一个类的
Hello.java
class Hello{ public static void main(String[] agrs){ System.out.println("hello"); } }
生成class javac Hello.java
打包 jar -cvf hello.jar Hello.class
说明:
c表示要创建一个新的jar包
v表示创建的过程中在控制台输出创建过程的一些信息
f表示给生成的jar包命名
运行 java -jar hello.jar
提示 hello.jar中没有主清单属性
用压缩软件打开hello.jar,META-INF文件夹,里MENIFEST.MF,用记事本打开,添加Main-Class属性
修改为
Manifest-Version: 1.0 Created-By: 1.8.0_151 (Oracle Corporation) Main-Class: Hello
保存后运行java -jar hello.jar
输出Hello
2.两个类的
(1)简单的
World.java
class World{ public static String say(){ return "world"; } }
Hello.java
class Hello{ public static void main(String[] agrs){ System.out.println("hello "+World.say()); } }
都编译成class
手动添加META-INF/MANIFEST.MF
Manifest-Version: 1.0 Created-By: 1.8.0_151 (Oracle Corporation) Main-Class: Hello
打包 jar -cvfm hello.jar META-INF\MANIFEST.MF Hello.class World.class
运行 java -jar hello.jar
输出 hello world
(2)带包名的打包
com/World.java
package com; public class World{ public static String say(){ return "world"; } }
编译为class
Hello.java
import com.World; class Hello{ public static void main(String[] agrs){ System.out.println("hello "+World.say()); } }
编译为class
运行 java Hello
输出 Hello World
打包 jar -cvfm hello.jar META-INF\MENIFEST.MF Hello.class com
运行java -jar hello.jar
输出 Hello World
但是hello.jar包里的com下包含World.class和World.java
优化:
新建target目录
javac com/World.java -d target
javac Hello.java -d target
复制目录加文件 META-INF/MANIFEST.MF
jar -cvfm hello.jar META-INF\MANIFEST.MF *
3.带jar的打包
(1)world.jar
com/World.java
package com; public class World{ public static String say(){ return "world"; } }
新建world目录
javac com/world.java -d world
在world下添加目录和文件 META-INF/MANIFEST.MF
MANIFEST.MF的内容
Manifest-Version: 1.0
Created-By: 1.8.0_151 (Oracle Corporation)
进入world 目录
执行打包 jar -cvfm world.jar META-INF\MANIFEST.MF *
(2)Hello.jar
import com.World; class Hello{ public static void main(String[] agrs){ System.out.println("hello "+World.say()); } }
新建target
将Hello.java和world.jar复制到target下,进入target目录
编译,生成class javac -cp world.jar Hello.java
删除Hello.java
添加目录和文件 META-INF/MANIFEST.MF
MANIFEST.MF的内容
Manifest-Version: 1.0 Created-By: 1.8.0_151 (Oracle Corporation) Main-Class: Hello Class-Path: world.jar
打包 jar -cvfm hello.jar META-INF\MANIFEST.MF *
运行 java -jar hello.jar
输出 hello world
说明:
-cp 表示 -classpath,指的是把tom.jar加入classpath路径下
4.带资源的打包
(1)资源在jar内
Hello.java
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; class Hello{ public static void main(String[] args) throws Exception { Hello hello = new Hello(); InputStream is = hello.getClass().getResourceAsStream("aa.txt"); print(is); } public static void print(InputStream inputStream) throws Exception { InputStreamReader reader = new InputStreamReader(inputStream, "utf-8"); BufferedReader br = new BufferedReader(reader); String s = ""; while ((s = br.readLine()) != null) System.out.println(s); inputStream.close(); } }
新建resource
编译 javac Hello.java -d resource
添加目录和文件 META-INF/MANIFEST.MF
MANIFEST.MF的内容
Manifest-Version: 1.0 Created-By: 1.8.0_151 (Oracle Corporation) Main-Class: Hello
添加文本文件aa.txt,输入点内容
进入resource目录
打包 jar -cvfm hello.jar META-INF\MANIFEST.MF *
运行 java -jar hello.jar
输出 文件内容
(2)资源在另一个jar包内
<1>资源包reource.jar
新建目录 r1
添加文件 aa.txt
添加目录和文件 META-INF/MANIFEST.MF
MANIFEST.MF的内容
Manifest-Version: 1.0
Created-By: 1.8.0_151 (Oracle Corporation)
打包 jar -cvfm resource.jar META-INF\MANIFEST.MF *
<2> hello.jar
Hello.jave
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; class Hello{ public static void main(String[] args) throws Exception { Hello hello = new Hello(); InputStream is = hello.getClass().getResourceAsStream("aa.txt"); print(is); } public static void print(InputStream inputStream) throws Exception { InputStreamReader reader = new InputStreamReader(inputStream, "utf-8"); BufferedReader br = new BufferedReader(reader); String s = ""; while ((s = br.readLine()) != null) System.out.println(s); inputStream.close(); } }
resource目录只保留META-INF/MANIFEST.MF,内容为
Manifest-Version: 1.0 Created-By: 1.8.0_151 (Oracle Corporation) Main-Class: Hello Class-Path: resource.jar
添加resource.jar
编译 javac -cp resource/resource.jar Hello.java -d resource
打包 jar -cvfm hello.jar META-INF\MANIFEST.MF *
(3)资源在jar包外
Hello.java
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; class Hello { public static void main(String[] args) throws Exception { Hello hello = new Hello(); InputStream is = new FileInputStream("aa.txt"); print(is); } public static void print(InputStream inputStream) throws Exception { InputStreamReader reader = new InputStreamReader(inputStream, "utf-8"); BufferedReader br = new BufferedReader(reader); String s = ""; while ((s = br.readLine()) != null) System.out.println(s); inputStream.close(); } }
编译javac Hello.java -d resource
进入resource
MANIFEST.MF内容为
Manifest-Version: 1.0 Created-By: 1.8.0_151 (Oracle Corporation) Main-Class: Hello
打包 jar -cvfm hello.jar META-INF\MANIFEST.MF *
新建aa.txt,输入内容
运行 java -jar hello.jar
输出aa.txt的内容
注意:
META-INF/MANIFEST
Manifest-Version: 1.0 Main-Class: package.classname Class-Path: a.jar b.jar
后面跟着一个英文的冒号,冒号后面必须跟着一个空格
Class-Path中的各项应使用空格分隔,不是逗号或分号
Class-Path中如果有很多项,写成一行打包的时候会报错line too long,这时需要把Class-Path分多行写
从第二行开始,必须以两个空格开头,一个空格结尾
Class-Path写完之后最后一定要有一个空行
说明:
程序中System.out.println输出中文乱码的解决方法
编译时指定编码为UTF-8
javac -encoding utf-8 Hello.java