示例:[ 自定义的接口,用jar包实现 ]
One:
- package com.one;
- public interface Action {
- public final String HELLO="hello"
- public String Hello();
- }
Two:
- public class Test implements Action{
- public String Hello() {
- return HELLO;
- }
- public static void main(String[] args) {
- Test test=new Test();
- System.out.println(test.Hello());
- }
- }
l 编译One 程序,并打包到com/one 目录下;
l 编写[ @.mf ] 文件,其中@表示任意名称;
main-class: com.one.Action [ 打包成jar的主类 ]
保存为 action.mf [ 可自行取名 ]
l 将第一步所得的文件带目录和第二步所得的文件一起放入一个文件夹
本例放入packaJar文件下
l 在Dos 下进入packJar所在的文件夹 [ 本例文件夹放在c:/packJar,可以任意位置 ]
C:\Documents and Settings\admin>cd\
C:\>cd packJar
C:\packJar>dir
2009-02-17 16:22 action.mf
2009-02-17 16:51 <DIR> com
C:\packJar>
l C:\packJar>jar cfm Action.jar action.mf com/one/*.class
Jar:打包命令
Cfm:参数
Action.jar:欲打成包的jar包名 [ 任意名 ]
Action.mf:……
Com/one/*.class:将com/one文件夹下的所有.class文件打入jar包中
l 成功后得:
l 将此jar包路径加入classpath环境变量中
l ---------- 编译 Two ----------
输出完成 (耗时: 0 秒) - 正常终止
l ---------- 运行 ----------
hello
输出完成 (耗时: 0 秒) - 正常终止
l 打包,测试成功