coolfiry

认认真真做人,兢兢业业做事!
posts - 39, comments - 17, trackbacks - 0, articles - 0

java打包详解

Posted on 2006-10-12 18:40 Coolfiry 阅读(337) 评论(0)  编辑  收藏 所属分类: Java
 和 'f'标志指定的相同顺序。

示例1:将两个class文件存档到一个名为 'classes.jar' 的存档文件中:
 & nbsp;     jar cvf& nbsp;classes.jar Foo.class Bar.class
示例2:用一个存在的清单(manifest)文件 'mymanifest' 将 foo/& nbsp;目录下的所有
            文件存档到一个名为  'classes.jar' 的存档文件中:
        jar cvfm  classes.jar mymanifest -C foo/ .

来个小例子试试看:
我们只有一个HelloWorld,如下:
public& nbsp; class  HelloWorld{
 public& nbsp;static void main(String[] args){
 System.out.println(“Hi, Hello World!”);
}
}
我将这个java文件存到C盘跟目录下,ok,接下来,
在先前打开的命令提示符下(跳转到C盘提示符下),我们输入javac HelloWorld.java,然后继续输入:jar & nbsp;cvf  hello.jar  HelloWorld.class,回车后去你的C盘看看,多了什么,没错 hello.jar 。
基本的步骤我们现在都知道了,你可以自己去尝试一下随着jar后面的参数的不同,结果有什么变化。
紧接着我们看看如何运行我们的jar包。
在进入正题之前,你要先打开我们刚刚做好的jar包看看,多了什么呢,META-INF目录?再看看里面是什么,还有一个MANIFEST.MF文件是不是?用文本编辑器(我这里是UltraEdit)打开它看看:
Manifest-Version:  1.0
Created-By: 1.4.2 (Sun Microsystems& nbsp;Inc.)
就是这样。这里我们对它进行修改,加一句:Main-Class:  HelloWorld (在第三行)。这个就是我们之前写的那个类,也就是我们的入口类。也即,
Manifest -Version: 1.0
Created-By: 1.4.2 (Sun& nbsp;Microsystems Inc.)
Main-Class: HelloWorld< BR>接下来,我们在命令提示符里执行:
jar umf  MANIFEST.MF app.jar
这样我们使用了我们自己的MANIFEST.MF文件对原来默认的进行了更新。你不妨可以再进去看看是不是添上了Main-Class: HelloWorld这一句。
Ok,这个最后的一步了,来验证我们做的一切,在命令提示符中输入:
java -jar hello.jar (执行)
出现了什么,――Hi, Hello World!
我们再来看看 jar文件在tomcat中发布,注意:在tomcat中我们就不能再用jar这种格式,而改war格式,它是专门用于web应用的,其实整个过程下来基本上和jar是类似的:
先准备我们要打包的资源。
找到存放tomcat的webapps目录,进到其中,新建一个文件夹,这里命名为hello,再进去新建WEB-INF文件夹,再进去新建classes文件夹,此时我们也将我们唯一的servlet, HelloWorld.java放到这里,在与classes目录同级下建立一文件web.xml。Ok,目前我们初步建立了一个简单的web应用。
这是HelloWorld.java:
import java.io.*;< BR>import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends& nbsp;HttpServlet {
 public void  doGet(HttpServletRequest req, HttpServletResponse  res)
              & nbsp;               & nbsp; throws ServletException, IOException& nbsp;{
  res.setContentType("text/html");
  PrintWriter out = res.getWriter ();
  out.println("<HTML>");< BR>  out.println("<HEAD><TITLE& gt;Hello, World!</TITLE></HEAD>");< BR>  out.println("<BODY>");
& nbsp; out.println("Hello, World!");
   out.println("</BODY></HTML>");
 }
}//end here!
对它编译。下面是web.xml:< BR><?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
  '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
  'http://java.sun.com/j2ee/dtds/web-app_2_3.dtd&< /FONT>#39;>
<web-app>
  <servlet>
     <servlet-name>hello</servlet-name& gt;
    <servlet-class& gt;HelloWorld</servlet-class>
   </servlet>
  <servlet-mapping& gt;
 <servlet-name>hello</servlet- name>
 <url-pattern>/HelloWorld& lt;/url-pattern>
  </servlet-mapping& gt;
</web-app>
开始压缩,形成war档:
在命令提示符下进到先前创制的hello目录下,执行 jar  cvf   hello.war  * ,我们便得到hello.war。将它拷贝至 webapps目录下,ok,来看最后一步,打开tomcat的目录conf中的server.xml,加入:
   <Context path="/hello" docBase="hello.war"& nbsp;debug="0"
    reloadable ="true"/>
大功告成!运行它,启动tomcat,后在浏览器中输入http://localhost:8080/hello/HelloWorld,有了吗?
最后,如果你想用ant来完成以上的打包活动,下面就告诉你:
对于jar来说。在build.xml 中,
 <target name="jar">< BR>  <jar destfile="${app_home}/hello.jar"& gt;
   <fileset dir=" ${dest}" includes="**"/>
  & nbsp;   <!--fileset dir="${dest} " includes="**/action.properties"/-->
     </jar>
 & lt;/target>

对于war,
<war& nbsp;warfile="hello.war" webxml="./WEB-INF/web.xml">
    <fileset dir="html"/& gt;
    <lib  dir="lib/">
    & nbsp;   <exclude name="oracle*.jar"/& gt;
    </lib>
    <classes  dir="build/servlets">
   & nbsp;     <include& nbsp;name="**/*.class"/>
  </classes& gt;
</war> 
好了,就这么多,希望对你有点帮助。:)
我上传了上面打过的两个包,hello.jar和hello.war。『 点击下载』
『 点击下载』
第一rar文件对应的是hello.jar,下载后将其名改为 hello.jar
第二rar文件对应hello.war,下载后改为hello.war。
这是由于上传不了 jar格式和war格式的文件,你只好照我上面说的去做了 :)

补充:
############
jar基本操作:
############
1.& nbsp;创建jar文件
  jar cf jar- file input-file(s)
c---want to Create& nbsp;a JAR file.
f---want the  output to go to a file  rather than to stdout.
eg: 1) jar cf myjar.jar query_maintain_insert.htm< BR>    2)jar cvf  myjar.jar query_maintain_insert.htm
       v---Produces verbose(详细的) output.
    3)jar& nbsp;cvf myjar.jar query_maintain_insert.htm mydirectory< BR>    4)jar cv0f  myjar.jar query_maintain_insert.htm mydirectory
      0---don't& nbsp;want the JAR file to be& nbsp;compressed.
    5)jar& nbsp;cmf MANIFEST.MF myjar.jar yahh.txt
      m---Used& nbsp;to include manifest information  from an existing manifest file.
    6)jar cMf MANIFEST.MF& nbsp;myjar.jar yahh.txt
   & nbsp;  M---the default manifest  file should not be produced.
    7)jar cvf myjar.jar& nbsp;*
       *---create all contents in current& nbsp;directory.
2. 察看jar文件
 & nbsp;jar tf jar-file
t---want to& nbsp;view the Table of contents  of the JAR file.
eg: 1)jar& nbsp;vft yahh.jar
       v---Produces verbose(详细的)  output.
3. 提取jar文件
   jar xf jar-file [archived-file(s)]
x- --want to extract files from  the JAR archive.
eg: 1)jar xf& nbsp;yahh.jar yahh.txt(仅提取文件yahh.txt)
 & nbsp;  2)jar xf yahh.jar alex/yahhalex.txt (仅提取目录alex下的文件yahhalex.txt)
   & nbsp;3)jar xf yahh.jar(提取该jar包中的所有文件或目录)
4. 修改Manifest文件
  jar  cmf manifest-addition jar-file input-file(s)< BR>m---Used to include manifest information& nbsp;from an existing manifest file.< BR>5. 更新jar文件
  jar  uf jar-file input-file(s)
u---want to update a

只有注册用户登录后才能发表评论。


网站导航: